var XMLHttpRequestObject		= false; 

if (window.XMLHttpRequest) {
    XMLHttpRequestObject		= new XMLHttpRequest();
} else if (window.ActiveXObject) {
    XMLHttpRequestObject		= new ActiveXObject("Microsoft.XMLHTTP");
}

function swap(id,img){
	if(id=='main-image'){
		document.getElementById(id).style.background='url('+img+');';
	}else{
		document.getElementById(id).src=img;
	}
}

function stopClick(e){
	var rightclick;
	if (!e) var e = window.event;
	if (e.which) rightclick = (e.which == 3);
	else if (e.button) rightclick = (e.button == 2);
	if(rightclick==true){
//		return false;
	}
}

function findCollection(){
	var f = document.collectionform;
	var s = f.selector.selectedIndex;
	var v = f.selector.options[s].value;
	var p = v.split(':');
	if(p[0]=='tag'){
		location.href='shop.php?tags='+p[1];
	}
	if(p[0]=='collection'){
		location.href='shop.php?showcat='+p[1];
	}
	if(p[0]=='color'){
	}
}

function showImage(id,img){
	if(window.event){
	var x = window.event.x-30;
	var y = window.event.y+10;
	document.getElementById(id).innerHTML='<img src="images/product/'+img+'" style="border:dotted 1px #AAA;background:#FFF;padding:5px;"/>';
	document.getElementById(id).style.visibility='visible';
	document.getElementById(id).style.left=x+'px';
	document.getElementById(id).style.top=y+'px';
	}
}
function hideImage(id){
	document.getElementById(id).innerHTML='';
	document.getElementById(id).style.visibility='hidden';
	document.getElementById(id).style.left='0px';
	document.getElementById(id).style.top='-1000px';
}
function removeItem(tmpid){
        var remurl='cart_removeitem.php?tmpid='+tmpid;
        getData(remurl,'subtotal','','');
        document.getElementById("cartitem"+tmpid).style.display='none';
        if(parseInt(document.getElementById("subtotal").innerHTML)==0){
		document.getElementById("totalsrow").innerHTML='<tr style="background:#FFF;border:dotted 1px #555;height:60px;"><td colspan="6" style="text-align:center;vertical-align:middle;">No items in your shopping cart at the moment.</td></tr>';
        }else{
		updateShipping();
        }
}
function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)){
		num = "0";
	}
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++){
		num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
	}
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}
function showZoneRates(){
	var sc = document.totals.country.selectedIndex;
	var zone = document.totals.country.options[sc].value.split('|');
	for(z=0;z<=2;z++){
		document.getElementById("zoneship"+z).style.display='none';
	}
	document.getElementById("zoneship"+zone[1]).style.display='block';
}

function updateShipping(){
	var si = document.totals.shipping.selectedIndex;
	var sv = document.totals.shipping.options[si].value.split('|');
	var sc = sv[1];
	document.ppForm.shipping.value = sc;
	document.ppForm.shiptype.value = sv[0];
	document.dForm.shipping.value = sc;
	document.dForm.shiptype.value = sv[0];
	var dsct = 0;
	if(document.getElementById("discount").innerHTML != '--'){
		dsct = parseFloat(document.getElementById("discount").innerHTML);
	}
	var ot = parseFloat(document.getElementById("subtotal").innerHTML) + parseFloat(sc) - dsct;
	//var ot = parseFloat(document.getElementById("subtotal").innerHTML) + parseFloat(sc) - parseFloat(document.getElementById("discount").innerHTML);
	//if(ot<0){
	//	ot = 0;
	//}	
	document.getElementById("shipcost").innerHTML = formatCurrency(sc);
	document.getElementById("ordertotal").innerHTML = formatCurrency(ot);
}
function updateShippingRadio(f){
    var val = f.value;
	var sv = val.split(',');
	var sm = sv[1];
	var sc = sv[2];
	var dsct = 0;
	if(document.getElementById("discount").innerHTML != '--'){
		dsct = parseFloat(document.getElementById("discount").innerHTML);
	}
	var ot = parseFloat(document.getElementById("subtotal").innerHTML) + parseFloat(sc) - dsct;
	//var ot = parseFloat(document.getElementById("subtotal").innerHTML) + parseFloat(sc) - parseFloat(document.getElementById("discount").innerHTML);

	
	document.ppForm.shipping.value = sc;
	document.dForm.shipping.value = sc;

	document.ppForm.shipmethod.value = sm;
	document.dForm.shipmethod.value = sm;
	
	document.dForm.AMT.value = ot;
	document.ppForm.AMT.value = ot;

	document.dForm.paymentAmount.value = ot;
	document.ppForm.paymentAmount.value = ot;

	//if(ot<0){
	//	ot = 0;
	//}
	document.getElementById("shipcost").innerHTML = formatCurrency(sc);
	document.getElementById("ordertotal").innerHTML = formatCurrency(ot);
}
function cartAdd(){
        var f=document.cartform;
        var sku=f.sku.value;
        var productid=f.productid.value;
        var variationid=f.variationid.value;
        var variationname=f.variationname.value;
        var custom=f.custom.value;
        var variation=f.variation.value;
        var qty=parseInt(f.qty.value);
        var psi=f.productstyle.selectedIndex;
        var pse=f.productstyle.options[psi].value;
        var psdata=pse.split('|');
        var unitprice=psdata[0];
        var pricetype=psdata[1];
        var customization='';
        if(f.customization){customization=f.customization.value;}
	if(custom){
		variationname+=' ['+custom;
		if(f.customization){ variationname+=' customized'; }
		variationname+=']';
	}
        if(qty>0){
                var addurl='cart_additem.php?sku='+sku+'&productid='+productid+'&variationid='+variationid+'&variation='+variation+'&variationname='+variationname+'&qty='+qty+'&unitprice='+unitprice+'&pricetype='+pricetype+'&customization='+customization;
		//alert(addurl);                
		getData(addurl,'carting','Adding...','Unable to add to cart');
        }
}
function ncartAdd(fi){
	var f=document.getElementById(fi);
	var sku=f.sku.value;
	var productid=f.productid.value;
	var variationid=f.variationid.value;
	var variationname=f.variationname.value;
	var custom=f.custom.value;
	if(f.name1){ custom = f.name1.value; /*alert('A wedding banner eh?');*/ }
	if(f.name2){ custom += ', '+f.name2.value; }
	if(f.date){ custom += ', '+f.date.value; }
	
	var variation=f.variation.value;
	var qty=parseInt(f.qty.value);
	
	if(f.productstyle.length>1){
		var checkTest = 0;
		for(a=0;a<f.productstyle.length;a++){
			if(f.productstyle[a].checked==true){
				checkTest++;
			}
		}
		if(checkTest==0){
			alert('Please indicate the size of print you want.');
			return false;
		}
	}
	if(f.childname){
		if(f.childname.value==''){
			alert('Please indicate the name you want\non your print.');
			return false;			
		}
	}
	if(f.groom && f.bride){
		if(f.groom.value=='' && f.bride.value==''){
			alert('Please indicate the names you want\non your print.');
			return false;		
		}else if(f.groom.value=='' && f.bride.value!=''){
			alert('Please indicate both of the names you\nwant on your print.');
			return false;		
		}else if(f.groom.value!='' && f.bride.value==''){
			alert('Please indicate both of the names you\nwant on your print.');
			return false;		
		}
	}
	
	if(f.productstyle.value==undefined){
		for(a=0;a<f.productstyle.length;a++){
			if(f.productstyle[a].checked==true){
				var pse=f.productstyle[a].value;
			}
		}
	}else{
		var pse = f.productstyle.value;
	}
	
	//var pse=f.productstyle.value;
	var psdata=pse.split('|');
	var unitprice=psdata[0];
	var pricetype=psdata[1];
	var customization='';
	if(f.customization){customization=f.customization.value;}
	if(f.customization && f.name1){ customization=custom; }
	if(f.customization && f.bride && f.groom && f.middle ){ 
		fsi = f.middle.selectedIndex;
		custom=f.bride.value+'*'+f.groom.value; 
		if(fsi==1){custom=f.bride.value+'-'+f.groom.value; }
		customization=f.bride.value+' '+f.middle.options[fsi].value+' '+f.groom.value; 
	}
	if(custom){
		custom = custom.replace(/\'/g,"`");
		variationname+=' ['+custom;
		if(f.customization && !f.name1){ variationname+=' customized'; }
		variationname+=']';
	}
	if(qty>0){
		if(pse==undefined){
			alert('Please select a price/size option before\nbefore adding it to your cart.');
		}else{
			var addurl='cart_additem.php?sku='+sku+'&productid='+productid+'&variationid='+variationid+'&variation='+variation+'&variationname='+variationname+'&qty='+qty+'&unitprice='+unitprice+'&pricetype='+pricetype+'&customization='+customization;
			getData(addurl,'carting','Adding...','Unable to add to cart');
		}
	}
}
function cartAdd2(){
        var f=document.cartform;
        var sku=f.sku.value;
        var productid=f.productid.value;
        var variationid=f.variationid.value;
        var variationname=f.variationname.value;
        var variation=f.variation.value;
        var qty=parseInt(f.qty.value);
        var psi=f.productstyle.selectedIndex;
        var pse=f.productstyle.options[psi].value;
        var psdata=pse.split('|');
        var unitprice=psdata[0];
        var pricetype=psdata[1];
	if(f.custom0.value!=''&&f.custom1.value!=''&&f.custom2.value!=''&&f.custom3.value!=''){
		variationname+=' ['+f.custom0.value+', '+f.custom1.value+', '+f.custom2.value+', '+f.custom3.value+']';
	}
	custom = custom.replace(/\'/g,"`");alert(custom);
        if(qty>0){
                var addurl='cart_additem.php?sku='+sku+'&productid='+productid+'&variationid='+variationid+'&variation='+variation+'&variationname='+variationname+'&qty='+qty+'&unitprice='+unitprice+'&pricetype='+pricetype;
                getData(addurl,'carting','Adding...','Unable to add to cart');
        }
}
function getNewsletter(){
	var field=document.newsletterform.email;
	var isEmail = field.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
	if(isEmail){
		var uri='newsletterSignup.php?email='+document.newsletterform.email.value;
		getData(uri,'newsletterform','sending to server...','unable to signup. mail to support@madebygirl.com');
	}
}
function login(){
	var uri='accountlogin.php?myemail='+document.loginform.email.value+'&mypassword='+document.loginform.password.value;
	getData(uri,'reseller-login','sending login info...','unable to log in. mail support@madebygirl.com');
}
function sendaFriend(sku){
	var field1=document.sendafriendform.myemail;
	var isEmail1 = field1.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
	var field2=document.sendafriendform.friendemail;
	var isEmail2 = field2.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
	if(isEmail1&&isEmail2){
		var uri='sendafriend.php?sku='+sku+'&myemail='+document.sendafriendform.myemail.value+'&friendemail='+document.sendafriendform.friendemail.value;
		getData(uri,'sendafriendform','sending to server...','unable to send. mail to support@madebygirl.com');
	}
}
function checkVal(field,val){
	if(field.value==val){
		field.value='';
	}
}
function getData(dataSource, divID, loadmsg, errmsg) { 
	var obj = document.getElementById(divID);
	obj.innerHTML = loadmsg;
	if(XMLHttpRequestObject) {
		XMLHttpRequestObject.open("GET", dataSource); 
		XMLHttpRequestObject.onreadystatechange = function() { 
		if ( XMLHttpRequestObject.readyState == 4 ) {
			var resp = XMLHttpRequestObject.responseText;
			if ( XMLHttpRequestObject.status == 200 ) {
				obj.innerHTML = resp;
			} else {
			        obj.innerHTML = errmsg;
				//obj.innerHTML = 'An error occurred on your last request.  <a href="#" onclick="swapDisplay(\'errDetails\',\'errToDo\');">Click here</a> to see details of the error.<br /><br /><div id="errToDo">If this error continues repeatedly, please contact <a href="mailto:support@istreamplanet.com" title="Email support">iStreamPlanet Tech Support</a></div><div id="errDetails" style="display:none;font:10px verdana;"><p style="width:400px;padding:5px;background-color:#EEE;">'+resp.replace("<font size=2 face='arial'>","<font size=1 face=verdana>")+'</p><p><a href="#" onclick="swapDisplay(\'errToDo\',\'errDetails\');">hide details</a></p></div>';
			}
		} 
	} 
	XMLHttpRequestObject.send(null);
	}
}
function beginCheckout18(acti){
	if(document.getElementById("shipcost").innerHTML != ''){
		var ot = document.getElementById("ordertotal").innerHTML;
		var st = document.getElementById("subtotal").innerHTML;
		var okToSubmit=false;
		ot = ot.replace('$','');
		if(acti==0){
        	okToSubmit=true;
		}else{
        	if(parseFloat(st)>=95){
            	okToSubmit=true;
        	}else{
            	alert('Wholesale order minimum is $100.');
        	}
		}
		document.ppForm.paymentAmount.value = ot;
		document.ppForm.AMT.value = ot;
		document.ppForm.dsct.value = document.getElementById("discount").innerHTML;
    	if(okToSubmit==true){
			var oktogo = confirm('Make sure your shipping address in PayPal is correct\nbefore completing your order!\n');
			if(oktogo){
        		document.ppForm.submit();
			}else{
				return false;
			}
		}
	}else{
		alert('Please select a shipping destination\nand method before continuing.');
		document.totals.country.focus();
	}
}

function beginCheckout28(acti){
	if(document.getElementById("shipcost").innerHTML != ''){
		var si = document.totals.shipping.selectedIndex;
		var ot = document.getElementById("ordertotal").innerHTML;
		var st = document.getElementById("subtotal").innerHTML;
		var okToSubmit=false;
		ot = ot.replace('$','');
		if(acti==0){
        	okToSubmit=true;
		}else{
        	if(parseFloat(st)>=95){
            	okToSubmit=true;
        	}else{
            	alert('Wholesale order minimum is $100.');
        	}
		}
		document.dForm.paymentAmount.value = ot;
		document.dForm.AMT.value = ot;
		var cs = document.getElementById("countrySelect").selectedIndex;
		document.dForm.country.value =  document.getElementById("countrySelect").options[cs].value;
		document.dForm.dsct.value = document.getElementById("discount").innerHTML;
    	if(okToSubmit==true){
        	document.dForm.submit();
		}
	}else{
		alert('Please select a shipping destination\nand method before continuing.');
		document.totals.country.focus();
	}
}

function beginCheckout(acti){
	var si = document.totals.shipping.selectedIndex;
	var ot = document.getElementById("ordertotal").innerHTML;
	var st = document.getElementById("subtotal").innerHTML;
	var okToSubmit=false;
	ot = ot.replace('$','');
	if(acti==0){
        okToSubmit=true;
	}else{
        if(parseFloat(st)>=95){
            okToSubmit=true;
        }else{
            alert('Wholesale order minimum is $100.');
        }
	}
	document.ppForm.paymentAmount.value = ot;
	document.ppForm.AMT.value = ot;
	document.ppForm.dsct.value = document.getElementById("discount").innerHTML;
    if(okToSubmit==true){
        document.ppForm.submit();
	}
}

function beginCheckout2(acti){
	var si = document.totals.shipping.selectedIndex;
	var ot = document.getElementById("ordertotal").innerHTML;
	var st = document.getElementById("subtotal").innerHTML;
	var okToSubmit=false;
	ot = ot.replace('$','');
	if(acti==0){
        okToSubmit=true;
	}else{
        if(parseFloat(st)>=95){
            okToSubmit=true;
        }else{
            alert('Wholesale order minimum is $100.');
        }
	}
	document.dForm.paymentAmount.value = ot;
	document.dForm.AMT.value = ot;
	document.dForm.dsct.value = document.getElementById("discount").innerHTML;
    if(okToSubmit==true){
        document.dForm.submit();
	}
}

function copyAddress(){
	var f = document.checkoutform;
	if(f.shipbillsame.checked==true){
		f.sname.value = f.firstName.value+' '+f.lastName.value;
		f.saddress1.value = f.address1.value;
		f.saddress2.value = f.address2.value;
		f.scity.value = f.city.value;
		f.szip.value = f.zip.value;
		f.scountry.selectedIndex = f.country.selectedIndex;
		f.sstate.selectedIndex = f.state.selectedIndex;
	}else{
		if(f.saddress1.value==f.address1.value){
			f.saddress1.value = '';
			f.saddress2.value = '';
			f.scity.value = '';
			f.szip.value = '';
			f.scountry.selectedIndex = 0;
			f.sstate.selectedIndex = 0;
		}
	}
}

function checkoutVerify(){
	var f = document.checkoutform;
	var errs = 0;
	var missing = '';
	if(f.email.value==''){errs++;missing+='- Your Email Address\n';}
	if(f.firstName.value==''){errs++;missing+='- Your First Name\n';}
	if(f.lastName.value==''){errs++;missing+='- Your Last Name\n';}
	if(f.zeroorder.value==0){
		if(f.creditCardNumber.value==''){errs++;missing+='- Your Credit Card Number\n';}
		if(f.cvv2Number.value==''){errs++;missing+='- Your Card Verification Number\n';}
	}
	if(f.address1.value==''){errs++;missing+='- Your Billing Address\n';}
	if(f.city.value==''){errs++;missing+='- Your Billing City\n';}
	if(f.zip.value==''){errs++;missing+='- Your Billing Zip Code\n';}
	if(f.country.selectedIndex==0){errs++;missing+='- Your Billing Country\n';}
	if(f.saddress1.value==''){errs++;missing+='- Your Shipping Address\n';}
	if(f.scity.value==''){errs++;missing+='- Your Shipping City\n';}
	if(f.scountry.selectedIndex==0){errs++;missing+='- Your Shipping Country\n';}
	if(errs){
		alert('Please correct the following errors before\nclicking "Pay Now":\n\n'+missing);
		return false;
	}else{
		//f.doButton.style.disabled=true;
		f.submit();
		return true;
	}
}

function checkAccountSignupForm(){
	var f = document.accountform;
	var ci = f.shipcountry.selectedIndex;
	var cv = f.shipcountry.options[ci].value;
	var em = 'Please complete all required fields before submitting your request.'
	var e = 0;
	//f.submitter.disabled==true;
	if(f.shipcity.value==''){e++;f.shipcity.focus();}
	if(f.shipaddress.value==''){e++;f.shipaddress.focus();}
	if(f.phone.value==''){e++;f.phone.focus();}
	//if(f.website.value==''){e++;f.website.focus();}
	if(f.email.value==''){e++;f.email.focus();}
	if(f.company.value==''){e++;f.company.focus();}
	if(f.name.value==''){e++;f.name.focus();}
	if(cv=='US'){
		if(f.resellerid.value==''){e++;f.resellerid.focus();em+='<br/>U.S. retailers must provide tax id# or reseller id.';}
	}
	if(e==0){
		f.submit();
	}else{
		$('#instruct').html('<span style="color:#fd377f;font-size:120%;"><strong>'+em+'</span>');
		f.submitter.disabled==false;
	}
}

