

function replaceSubstring(inputString, fromString, toString) {
     // Goes through the inputString and replaces every occurrence of fromString with toString
     var temp = inputString;
    if (fromString == "") {
      return inputString;
    }
 if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
          var toTheLeft = temp.substring(0, temp.indexOf(fromString));
          var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
          temp = toTheLeft + toString + toTheRight;
     }
    } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
       var midStrings = new Array("~", "`", "_", "^", "#");
       var midStringLen = 1;
       var midString = "";
       // Find a string that doesn't exist in the inputString to be used
       // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
          var tempMidString = "";
              for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
              if (fromString.indexOf(tempMidString) == -1) {
                midString = tempMidString;
                 i = midStrings.length + 1;
            }
          }
       }  // Keep on going until we build an "inbetween" string that doesn't exist
       // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
       while (temp.indexOf(fromString) != -1) {
       var toTheLeft = temp.substring(0, temp.indexOf(fromString));
        var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
        temp = toTheLeft + midString + toTheRight;
       }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
      var toTheLeft = temp.substring(0, temp.indexOf(midString));
       var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
          temp = toTheLeft + toString + toTheRight;
      }
    }  // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp;  // Send the updated string back to the user
 }  // Ends the "replaceSubstring" function


function update_item() {
    var answer = '';
    for (var i = 0; i<document.details.elements.length; i++) {
        if ((document.details.elements[i].type == 'select-one')) {
            if (document.details.elements[i].selectedIndex != 0) {
                //answer += document.formName.elements[i].options[document.formName.elements[i].selectedIndex].value + ',';
				answer += document.details.elements[i].name;
            }
        }
    }
	
    items = replaceSubstring(answer, ":qnty", ",") 
	theLength = (items.length) - 1;
	items = items.slice(0,theLength);
	
	document.details.sku.value = items;
	
	
	}

function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }

function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}



/* -------------------- samples -----------------*/


// selectSample classes



function selectShade(divId){

	var ele = document.getElementById(divId);
	
	if(ele.style.display == "block") {
    		ele.style.display = "none";
		
  	}
	else {
		ele.style.display = "block";
		
	}


}

function selectSample(divId){
 var e = document.getElementById('sample' + divId);
 var c = e.getAttribute("class");
 
  if(c == "sampleItem"){
 	e.setAttribute("class", "sampleItemSelected");
 	e.setAttribute("className", "sampleItemSelected"); // for IE which does not recognize "class";
 }

 else{
 	e.setAttribute("class", "sampleItem");
 	e.setAttribute("className", "sampleItem"); // for IE which does not recognize "class";
 }
 
 //turn off shades
 var vShades = document.getElementById('veil');
 var iShades = document.getElementById('illusion');
 vShades.style.display = "none";
 iShades.style.display = "none";
 
}




function getSamples(){
//get samples
selectedValue = "";
j=0;
for(i=0; i<15; i++){
	var e = document.getElementById('sample' + i);
 	var c = e.getAttribute("class");
 	if(c == "sampleItemSelected"){
 	selectedValue += i+"|";
 	j++;
 	}
}


if(j==0){
	alert('Please select a sample');
}
else if(j>3){
	alert('You may only select 3 samples');
}

else{
	LoadPage('sampleInfo.php?samples='+selectedValue, 'sampleOptions');
	 var sa = document.getElementById('sampleArea');
 	 sa.style.display = "none";
}
}



function LoadPage(page,usediv) {
         // Set up request varible
         try {xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");}  catch (e) { alert("Error: Could not load page.");}
         //Show page is loading
         document.getElementById(usediv).innerHTML = '<img src="../_img/ajax-loader.gif" /> Loading Page...';
         //scroll to top
         scroll(0,0);
         //send data
         xmlhttp.onreadystatechange = function(){
                 //Check page is completed and there were no problems.
                 if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
                        //Write data returned to page
                        document.getElementById(usediv).innerHTML = xmlhttp.responseText;
                 }
         }
         xmlhttp.open("GET", page);
         xmlhttp.send(null);
         //Stop any link loading normaly
         return;
}



function validateForm(id){
	
	valid = true;	
	//get the fields
	
	
	
	
	if(document.getElementById('firstName').value==""){
		valid = false;
		alert('Please enter you name');
	}
	
	
	else if(document.getElementById('email').value==""){
		valid = false;
		alert('Please enter you email');
	}
	
	else if(document.getElementById('friendFirstName').value==""){
		valid = false;
		alert('Please enter your friends first name');
	}
	
	else if(document.getElementById('friendLastName').value==""){
		valid = false;
		alert('Please enter your friends last name');
	}
	
	else if(document.getElementById('friendAddress').value==""){
		valid = false;
		alert('Please enter your friends address');
	}
	
	else if(document.getElementById('friendCity').value==""){
		valid = false;
		alert('Please enter your friends city');
	}
	
	
	else if(document.getElementById('friendState').value==""){
		valid = false;
		alert('Please enter your friends state');
	}
	
	else if(document.getElementById('friendZip').value==""){
		valid = false;
		alert('Please enter your friends zip code');
	}
	
	else if(document.getElementById('friendEmail').value==""){
		valid = false;
		alert('Please enter your friends email');
	}
	

	
	if(valid == true){
		 submitForm(id);
	}

	
}


function submitForm(id){
      e = document.getElementById(id);
      e.submit();
      
      
      
      }



