// load items based on selected parent
function changeStoreViews(parent,item_element,post_file,wait_msg,noitem_msg) {
	if(parent!=""){
		var wait_note   = '<option>'+wait_msg+'</option>'; // wait message
	    var zero_note   = noitem_msg;  // no item message
		jQuery(item_element).html(wait_note);
	    post_file = post_file+"id/"+parent;
	    jQuery.post(post_file, { },
	        function(data)
	            {
	    			var opt = "";
	                if(data.length)
	                {
	                    for(i=0;i<data.length;i++) {
	                        opt += '<option value="' + data[i].code + '">' + data[i].name + '</option>';
	                    }
	                    jQuery(item_element).attr("disabled",false);
	                }
	                else
	                {
	                	jQuery(item_element).attr("disabled",true);
	                	opt ='<option value="">'+zero_note+'</option>';
	                }
	                jQuery(item_element).html(opt);
	            }
	        ,'json');
	}
}

function moveSelectedValues(from,to,hiddenId,cond){
	var fromElement = document.getElementById(from);
	var toElement = document.getElementById(to);
	var assocIds = document.getElementById(hiddenId).value;
	for(var i=fromElement.options.length-1;i>=0;i--){
		if(fromElement.options[i].selected){
			var optn = document.createElement("OPTION");
			optn.text = fromElement.options[i].text;
			optn.value = fromElement.options[i].value;
			toElement.options.add(optn);
			if(cond=='add'){
				assocIds += optn.value+",";
			} else if(cond=='remove'){
				assocIds1 = assocIds.split(",");
				assocIds = "";
				for(var j=0;j<assocIds1.length;j++){
					if(assocIds1[j]!=optn.value && assocIds1[j]!=""){
						assocIds += assocIds1[j]+",";
					}
				}
			}
			
			fromElement.remove(i);
		}
	}	
	document.getElementById(hiddenId).value=assocIds;
	return false;
}

function moveSelectedValues1(from,to,hiddenId,cond){
	var fromElement = document.getElementById(from);
	var toElement = document.getElementById(to);
	var assocIds = document.getElementById(hiddenId).value;
	for(var i=fromElement.options.length-1;i>=0;i--){
		if(fromElement.options[i].selected){
			var optn = document.createElement("OPTION");
			optn.text = fromElement.options[i].text;
			optn.value = fromElement.options[i].value;
			if(toElement.name == 'associated_products[]')
			{
				assocIds2 = assocIds.split(",");
				for(var j=0;j<assocIds2.length;j++){
					if(assocIds2[j] == optn.value){
						val = Math.floor(Math.random()*25);
						optn.value = optn.value+'_'+val;
					}
				}
				toElement.options.add(optn);
			}	
				
			if(cond=='add'){
				assocIds += optn.value+",";
			} else if(cond=='remove'){
				assocIds1 = assocIds.split(",");
				assocIds = "";
				for(var j=0;j<assocIds1.length;j++){
					if(assocIds1[j]!=optn.value && assocIds1[j]!=""){
						assocIds += assocIds1[j]+",";
					}
				}
			}
				
				if(fromElement.name != 'products[]')
					fromElement.remove(i);
			
		}
	}	
	document.getElementById(hiddenId).value=assocIds;
	return false;
}

function storeViewProduct(str,path){
	if(str!=0){
		window.location.href = path+"storeview/"+str;
	}else{
		window.location.href = path;
	}
}
function stockViewProduct(str,path){
	if(str!=''){
		window.location.href = path+"stock/"+str;
	}else{
		window.location.href = path;
	}
}
function searchProducts(parent,item_element,post_file,wait_msg,noitem_msg,assoc_ids){
	parent = document.getElementById(parent).value;
	//if(parent!=""){
		var wait_note   = '<option>'+wait_msg+'</option>'; // wait message
	    var zero_note   = noitem_msg;  // no item message
		jQuery(item_element).html(wait_note);
	    post_file = post_file+"?product="+parent;
	    jQuery.post(post_file, { },
	        function(data)
	            {
	    			var opt = "";
	                if(data.length)
	                {
	                	assoc_ids = document.getElementById(assoc_ids).value;
	                	assoc_ids = assoc_ids.split(",");
	                	for(i=0;i<data.length;i++) {
	                		var visible = true;
	                		for(var j=0;j<assoc_ids.length-1;j++){
	                			if(data[i].code==assoc_ids[j]){
	                				visible = false;
	                			}
	                		}
	                		if(visible){
	                			//opt += '<option value="' + data[i].code + '" label="'+ data[i].name + '">' + data[i].name + '</option>';
	                			opt += '<option value="'+data[i].code+'" label="'+data[i].name+'">' + data[i].name + '</option>';
	                		}
	                    }
	                    jQuery(item_element).attr("disabled",false);
	                }
	                else
	                {
	                	jQuery(item_element).attr("disabled",true);
	                	opt ='<option value="">'+zero_note+'</option>';
	                }
	                jQuery(item_element).html(opt);
	            }
	        ,'json');
	//}
}

//load states based on country selection
function showStates(parent,item_element,post_file,wait_msg,noitem_msg) {
	if(parent!=""){
		var wait_note   = '<option>'+wait_msg+'</option>'; // wait message
	    var zero_note   = noitem_msg;  // no item message
		jQuery(item_element).html(wait_note);
		if(parent!=null)
		{
			post_file = post_file+"sid/"+parent;
		}
		
	    jQuery.post(post_file, { },
	        function(data)
	            {			    	
	    			var opt = "";
	                if(data.length)
	                {
	                    for(i=0;i<data.length;i++) {
	                        opt += '<option value="' + data[i].code + '" label="' + data[i].name + '">' + data[i].name + '</option>';
	                    }
	                    jQuery(item_element).attr("disabled",false);
	                }
	                else
	                {
	                	jQuery(item_element).attr("disabled",true);
	                	opt ='<option value="" label="'+ zero_note + '">'+zero_note+'</option>';
	                }
	                
	                jQuery(item_element).html(opt);
	            }
	        ,'json');
	}
}
//load states based on country selection
function showStatesFromZipcode(parent,item_element,post_file,wait_msg,noitem_msg) {
	
	if(parent!=""){
		
		document.getElementById('zip_err').style.display = 'none';
		
		var wait_note   = "<option>"+wait_msg+"</option>"; // wait message
		
	    var zero_note   = noitem_msg;  // no item message
		if(parent!="")

		{
			
			post_file = post_file+"sid/"+parent;
		}
		
		var stateabb ='';
		var city ='';
		var state = '';
		var cityopt = "";
		var stateopt = "";
		
	    jQuery.post(post_file, { },
	        function(data){
	    	
	    	if(data.length){
	    		for(i=0;i<data.length;i++) {	
	    			//cityopt += '<option value="' + data[i].city + '" label="' + data[i].city + '">' + data[i].city + '</option>';
	    			//stateopt += '<option value="' + data[i].stateabb + '" label="' +data[i].stateabb + '">' + data[i].state + '</option>';
	    			document.getElementById('city_id').value = data[i].city;
	    			document.getElementById('state_id').value = data[i].state;
	    			
	    			//cityopt += '<input type="text" value="' + data[i].city + '" label="' + data[i].city + '">' + data[i].city ;
	    			//stateopt += '<option value="' + data[i].stateabb + '" label="' +data[i].stateabb + '">' + data[i].state + '</option>';
	    		}
    		}else{
    			//document.getElementById('zip_err').innerHTML = '<ul class="valid_errors"><li>Zipcode Invalid</li></ul>';
    			document.getElementById('zip_err').style.display = 'block';
    			document.getElementById('city_id').value = '';
    			document.getElementById('state_id').value = '';	
    		}
	    	}
	        ,'json');
	}
}
function addTierPrice(path,waitmsg,notemsg)
{
	addRowToTable1(null,path,waitmsg,notemsg);
}
/////////////////////////////////////////////////////////////////////////////


//CONFIG notes. Below are some comments that point to where this script can be customized.
//Note: Make sure to include a <tbody></tbody> in your table's HTML


var RADIO_NAME1 = 'totallyrad'; // this is being set via script
var TABLE_NAME1 = 'tblSample'; // this should be named in the HTML
var ROW_BASE1 = 0; // first number (for display)
var hasLoaded1 = true;

var QTY_VALUE1 = 'qtyvalue';
var OPTION_VALUE1 = 'optionvalue';
var PRICE_VALUE1 = 'pricevalue';

var myFlag1;
//window.onload=fillInRows;

function fillInRows1()
{
	hasLoaded1 = true;
	addRowToTable1();
	addRowToTable1();
}

//CONFIG:
//myRowObject is an object for storing information about the table rows
function myRowObject1(one, two, three)
{
	this.one = one; // input text object
	this.two = two; // input text object
	this.three = three; // input button object
	
}

/*
* addRowToTable1
* Inserts at row 'num', or appends to the end if no arguments are passed in. Don't pass in empty strings.
*/

function addRowToTable1(num,path,waitmsg,notemsg)
{
		
	 	document.getElementById('optionvalue_err').style.display = "none";	 
		var inval = document.getElementById('mvalues').value;
		inval = parseInt(inval) + 1;
		document.getElementById('mvalues').value = inval;
		
	if (hasLoaded1) {
		var tbl = document.getElementById(TABLE_NAME1);
		var nextRow = tbl.tBodies[0].rows.length;
		
		var iteration = nextRow + ROW_BASE1;
		
		if (num == null) { 
			num = nextRow;
		} else {
			iteration = num + ROW_BASE1;
		}
		
		// add the row
		var row = tbl.tBodies[0].insertRow(num);
		
		// CONFIG: requires classes named classy0 and classy1
		row.className = 'classy' + (iteration % 2);
	
		// CONFIG: This whole section can be configured
		
		// cell 0 - text
		myFlag1 = OPTION_VALUE1 + iteration;	
		var cell0 = row.insertCell(0);
		var txtInp = document.createElement('select');
		txtInp.setAttribute('name', OPTION_VALUE1 + iteration);
		txtInp.setAttribute('id', OPTION_VALUE1 + iteration);
		txtInp.setAttribute('style', 'width:150px;');
		txtInp.setAttribute('class','input-text1');
		cell0.appendChild(txtInp);
		
		// cell 1 - input text
		var cell1 = row.insertCell(1);
		var txtInp1 = document.createElement('input');
		txtInp1.setAttribute('type', 'text');
		txtInp1.setAttribute('name', QTY_VALUE1 + iteration);
		txtInp1.setAttribute('id', QTY_VALUE1 + iteration);
		txtInp1.setAttribute('onkeypress',"javascript:this.value.replace('/^[0-9]$/','');");
		txtInp1.setAttribute('class','input-text_attrb1');
		txtInp1.setAttribute('size', '3');
		cell1.appendChild(txtInp1);
		var cellText = document.createTextNode(" and above");
		cell1.appendChild(cellText);	
		
		// cell 2 - input text
		var cell2 = row.insertCell(2);
		var txtInp2 = document.createElement('input');
		txtInp2.setAttribute('type', 'text');
		txtInp2.setAttribute('name', PRICE_VALUE1 + iteration);
		txtInp2.setAttribute('id', PRICE_VALUE1 + iteration);
		txtInp2.setAttribute('onkeypress',"javascript:this.value.replace('/^[0-9]$/','');");
		txtInp2.setAttribute('class','input-text_attrb2');
		txtInp2.setAttribute('size', '10');
		cell2.appendChild(txtInp2);
		
		
	

		// cell 3- input button
		var cell4 = row.insertCell(3);
		var btnEl = document.createElement('input');
		btnEl.setAttribute('type', 'button');
		btnEl.setAttribute('value', 'Delete');
		btnEl.setAttribute('class','dec_button');
		btnEl.onclick = function () {deleteCurrentRow(this)};
		cell4.appendChild(btnEl);
		
		// Pass in the elements you want to reference later
		// Store the myRow object in each row
		row.myRow = new myRowObject1(txtInp,txtInp1,txtInp2, btnEl);
		
		eval("var val = document.getElementById('"+OPTION_VALUE1+ iteration+"');");
		
		
		showStates(null,val,path,waitmsg,notemsg);
		
		//val.innerHTML ='<option value="">Please Select</option><option value="1">General</option>';


	}
}

//If there isn't an element with an onclick event in your row, then this function can't be used.
function deleteCurrentRow1(obj)
{
	if (hasLoaded1) {
		var delRow = obj.parentNode.parentNode;
		var tbl = delRow.parentNode.parentNode;
		var rIndex = delRow.sectionRowIndex;
		var rowArray = new Array(delRow);
		deleteRows1(rowArray);
		reorderRows1(tbl, rIndex);
	}
}

function reorderRows1(tbl, startingIndex)
{
	if (hasLoaded1) {
		if (tbl.tBodies[0].rows[startingIndex]) {
			var count = startingIndex + ROW_BASE1;
			for (var i=startingIndex; i<tbl.tBodies[0].rows.length; i++) {
				// CONFIG: next line is affected by myRowObject1 settings
				tbl.tBodies[0].rows[i].myRow.one.name = OPTION_VALUE1 + i; // input text
				tbl.tBodies[0].rows[i].myRow.one.id = OPTION_VALUE1 + i;
				
				// CONFIG: next line is affected by myRowObject1 settings
				tbl.tBodies[0].rows[i].myRow.two.name = QTY_VALUE1 + i; // input text
				tbl.tBodies[0].rows[i].myRow.two.id = QTY_VALUE1 + i; // input text
				
				// CONFIG: next line is affected by myRowObject1 settings
				tbl.tBodies[0].rows[i].myRow.two.name = PRICE_VALUE1 + i; // input text
				tbl.tBodies[0].rows[i].myRow.two.id = PRICE_VALUE1 + i; // input text


				// CONFIG: requires class named classy0 and classy1
				tbl.tBodies[0].rows[i].className = 'classy' + (count % 2);
				
				count++;
			}
		}
	}
	
	if( tbl.tBodies[0].rows.length == 1 )
		document.getElementById('mvalues').value = 0;
}

function deleteRows1(rowObjArray)
{
	if (hasLoaded1) {
		for (var i=0; i<rowObjArray.length; i++) {
			var rIndex = rowObjArray[i].sectionRowIndex;
			rowObjArray[i].parentNode.deleteRow(rIndex);
		}
	}
}
//check email availability
function existUserCode(item_element,parent,post_file,wait_msg,noitem_msg) {	
	
	
		if(parent!=""){
		
			var wait_note   = wait_msg; // wait message
		    var zero_note   = noitem_msg;  // no item message
			jQuery(item_element).html(wait_note);
			
		    post_file = post_file+"code/"+item_element;
		   
		    jQuery.post(post_file, { },
		        function(data)
		            {
		    			var opt = "";
		    			opt = data[0].name;		
		    				    			
		               if(opt!=''){
		            	   jQuery('#usercode_info').html("<font color=red>Code already Exist</font>");
		                    
		                }else{
		                	document.getElementById('usercode_info').innerHTML = '';   	
		                }
		                jQuery(item_element).html(opt);
		            }
		        ,'json');
		
			}
}
	function editbillingaddress(val,path){
		 if(val == 0){
			 document.location.href=path+"/flag/1";

			}	
	}
	