var focusimage = 1;
var ShipAndBillMsg,msgB,msgS;
var TypePost = new Array(0,0);
function changeimg(obj,i){
	document.products.frontimg.src=obj.src.replace("vsimg.php","simg.php");
	focusimage = i;
}
function maxsizeimg(obj,i){
	addadress = "&i="+focusimage;
	window.open("/slideimg.php?product="+obj+addadress,"","menubar=no,scrollbars=no,status=no,width=530,height=600");
	return false;
}
function scryti(ARR,obj){
if(obj.value=='Close')obj.value='Show';
else obj.value='Close';
	for(el in ARR){
		str =  ARR[el];
		str2=document.getElementById(str+'');
//		str2=document.str1;
		if(str2.style.display==''){
			str2.style.display='none';
		}else{
			str2.style.display='';
		}
	}
}
function open1(ARR){
	for(el in ARR){
		str =  ARR[el];
		str2=document.getElementById(str+'');
		str2.style.display='';
	}
}
function close1(ARR){
	for(el in ARR){
		str =  ARR[el];
		str2=document.getElementById(str+'');
		str2.style.display='none';
	}
}
function init(){
	close1(['tr2','tr3','tr4']);open1(['tr1']);
}
function CheckEmail(form) {
	var rejectedDomain=new Array()
	var index=0;
	rejectedDomain[index++]="zdnetmail"

	var rejected=false
	var testresults=true
	var str=form
	var filter=/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i
	if (filter.test(str)){
		var tempstring = str.split("@")
		tempstring = tempstring[1].split(".")
		for (i=0; i<rejectedDomain.length; i++) {
			if (tempstring[0]==rejectedDomain[i])
			rejected=true
		}
		if (rejected) {
			var message="Please input a more official email address!\n"
			message += "The following addresses are not allowed:\n"
			for (i=0; i<rejectedDomain.length; i++) {
				message += "\t" + rejectedDomain[i] + "\n"
			}
		//	alert( message) ;
			testresults=false
		}
	} else {
		message="Please input a complete and valid email address!"
	//	alert( message)
		testresults=false
	}
	return (testresults)
}
function CheckSimple(value)
{  // alert(value);
	var str=value;
	if(str.length<2) {return false; }
	else
	return true;
}
function CheckPhone(value)
{  // alert(value);
	var str=value;
	var filter=/^[0-9 \-\(\)\+]{5,17}$/i
	if(filter.test(str)){
		return true;
	}else return false;
}
function CheckName(value){
	// alert(value);
	var str=value;
	var filter=/^[a-zA-Z0-9\. \-\(\)\+]{2,17}$/i
	if(filter.test(str)){
		return true;
	}else return false;
}
function CheckAddress(value){
	// alert(value);
	var str=value;
	var filter=/^.{2,}$/i
	if(filter.test(str)){
		return true;
	}else return false;
}
function IfCorrect(correct,field){
	//alert(correct);
	obj=document.getElementById(field+'');
	//alert(obj);
	if(correct==true){
		obj.className='check_true';
	}else{
		obj.className='check_false';
	}
	return false;
}

var ExtDisabled = new Array(10);
//ExtDisabled['NextPersonal'] = false;
//ExtDisabled['NextBilling'] = false;
function EnableNext(arr,button1,rtype){
 //alert(arr);
 button1=document.getElementById(button1+'');
 //button1.disabled=false;
 if(ExtDisabled[button1]==undefined){
 	ExtDisabled[button1] = false;
 }
 ExtDisabled[button1.id] = false;
 button1.title="";
 shipandbill = 0;
 for(el in arr){
	str =  arr[el];
	if(str=='BILLADDRESS'){
		shipandbill = 1;
	}
	str2=document.getElementById(str+'');
	if(str2.className=="check_false"){
	  //button1.disabled=true;
	  ExtDisabled[button1.id] = true;
	  button1.title="Please check fields marked with red";
	  msg = "Please check fields marked with red";
	  if(shipandbill == 1){
	  	msg += '\n\n'+ShipAndBillMsg;
	  }
	  if(rtype==1){
	  	return false;
	  }else{
	  	alert(msg);
	  	break;
	  }
	}
 }
 return true;
}

function test_button_NextPersonal(t){
	EnableNext(['EMAIL','FNAME','LNAME','PHONE'],'NextPersonal',0);
	if(t==1) return false;
	if(ExtDisabled['NextPersonal']){
		return false;
	}else{
		close1(['tr2','tr1','tr4']);open1(['tr3']);
		return false;
	}
	return false;
}

function test_button_NextBilling(t){
	test_copy();
	shippandbill();
	EnableNext(['BILLADDRESS','BILLCITY','BILLSTATE','BILLCOUNTRY','BILLZIP','SHIPADDRESS','SHIPCITY','SHIPSTATE','SHIPCOUNTRY','SHIPZIP'],'NextBilling',0);
	if(t==1) return false;
	if(ExtDisabled['NextBilling']){
		return false;
	}else{
		close1(['tr1','tr3','tr2']);
		open1(['tr4']);
		return false;
	}
	return false;
}

function copy(){
 document.getElementById('tBILLADDRESS').value = document.getElementById('tSHIPADDRESS').value;
 document.getElementById('tBILLCITY').value    = document.getElementById('tSHIPCITY').value;
 document.getElementById('tBILLCOUNTRY').value = document.getElementById('tSHIPCOUNTRY').value;
 country_change('BILL');
 document.getElementById('tBILLSTATE').value   = document.getElementById('tSHIPSTATE').value;
 document.getElementById('tBILLZIP').value     = document.getElementById('tSHIPZIP').value;
 j=CheckAddress(document.getElementById('tBILLADDRESS').value); IfCorrect(j,'BILLADDRESS');
 j=CheckName(document.getElementById('tBILLCITY').value); IfCorrect(j,'BILLCITY');
 CheckStateCountry('b');
 j=CheckName(document.getElementById('tBILLZIP').value); IfCorrect(j,'BILLZIP');
}

function shippandbill_old(){
	msgB = '';
	msgS = '';
	ShipAndBillMsg = '';

	BillState = document.getElementById('tBILLSTATE');
	BillCountry = document.getElementById('tBILLCOUNTRY');
	ShipState = document.getElementById('tSHIPSTATE');
	ShipCountry = document.getElementById('tSHIPCOUNTRY');
	ship = document.getElementById('samewithship_id');
	if(BillCountry.value != '' && BillState.value!=''){
		if(BillCountry.value=='US' && BillState.value=='XX' && ship.checked==false){
			msgB += '  For '+BillCountry.options[BillCountry.selectedIndex].text+' you have to choose the state.\n';
		}
		if(BillCountry.value!='US' && BillState.value!='XX'){
			msgB += '  There is no such state in '+BillCountry.options[BillCountry.selectedIndex].text+'.\n';
		}
	}
	if(ShipCountry.value != '' && ShipState.value!=''){
		if(ShipCountry.value=='US' && ShipState.value=='XX'){
			msgS += '  For '+ShipCountry.options[ShipCountry.selectedIndex].text+' you have to choose the state.\n';
		}
		if(ShipCountry.value!='US' && ShipState.value!='XX'){
			msgS += '  There is no such state in '+ShipCountry.options[ShipCountry.selectedIndex].text+'.\n';
		}
	}

	if(msgB != ''){
		ShipAndBillMsg += 'Bill Information:\n'+msgB;
	}
	if(msgS != ''){
		ShipAndBillMsg += 'Ship Information:\n'+msgS;
	}
	return true;
	
}

function shippandbill(){
	msgB = '';
	msgS = '';
	ShipAndBillMsg = '';

	BillState = document.getElementById('tBILLSTATE');
	BillCountry = document.getElementById('tBILLCOUNTRY');
	ShipState = document.getElementById('tSHIPSTATE');
	ShipCountry = document.getElementById('tSHIPCOUNTRY');
	ship = document.getElementById('samewithship_id');
	if(BillCountry.value != '' && BillState.value!=''){
		if(CityToShop[BillCountry.value]==undefined){
			if(BillState.value!='XX' && ship.checked==false){
				msgB += '  For '+BillCountry.options[BillCountry.selectedIndex].text+' you have to choose the state.\n';
			}
		}else if(CityToShop[BillCountry.value][BillState.value]==undefined){
			msgB += '  For '+BillCountry.options[BillCountry.selectedIndex].text+' you have to choose the state.\n';
		}
	}
	if(ShipCountry.value != '' && ShipState.value!=''){
		if(CityToShop[ShipCountry.value]==undefined){
			if(ShipState.value!='XX'){
				msgS += '  For '+ShipCountry.options[ShipCountry.selectedIndex].text+' you have to choose the state.\n';
			}
		}else if(CityToShop[ShipCountry.value][ShipState.value]==undefined){
			msgS += '  For '+ShipCountry.options[ShipCountry.selectedIndex].text+' you have to choose the state.\n';
		}
	}

	if(msgB != ''){
		ShipAndBillMsg += 'Bill Information:\n'+msgB;
	}
	if(msgS != ''){
		ShipAndBillMsg += 'Ship Information:\n'+msgS;
	}
	return true;
	
}

function test_copy(){
	if(document.getElementById('samewithship_id').checked){
		copy();
	}
}

function CheckStateCountry(ltype){
	shippandbill();
	BillState = document.getElementById('tBILLSTATE');
	BillCountry = document.getElementById('tBILLCOUNTRY');
	ShipState = document.getElementById('tSHIPSTATE');
	ShipCountry = document.getElementById('tSHIPCOUNTRY');
	ship = document.getElementById('samewithship_id');
	if(ltype=='s'){
		if(msgS!='' || ShipCountry.value == '' || ShipState.value==''){
			IfCorrect(false,'SHIPSTATE');
			IfCorrect(false,'SHIPCOUNTRY');
		}else{
			IfCorrect(true,'SHIPSTATE');
			IfCorrect(true,'SHIPCOUNTRY');
		}
	}
	if(ltype=='b' || ship.checked==true){
		if(msgB!='' || BillCountry.value == '' || BillState.value==''){
			IfCorrect(false,'BILLSTATE');
			IfCorrect(false,'BILLCOUNTRY');
		}else{
			IfCorrect(true,'BILLSTATE');
			IfCorrect(true,'BILLCOUNTRY');
		}
	}
}

function set_postal(pref){
//	alert(pref);
	formax = 0;
	formax2 = 0;
	postsel = 0;
	prefback = '';
	if(pref=='us'){
		postsel = TypePost[0];
		prefback = 'int';
		formax = post_int;
		formax2 = post_us;
	}else if(pref=='int'){
		postsel = TypePost[1];
		prefback = 'us';
		formax = post_us;
		formax2 = post_int;
	}
	for(i=0;i<formax2;i++){
		if(document.getElementById(pref+'_'+i).checked){
			postsel = i;
		}
	}
	for(i=0;i<formax;i++){
		if(document.getElementById(prefback+'_'+i).checked){
			if(pref=='int'){
				TypePost[0] = i;
			}else if(pref=='us'){
				TypePost[1] = i;
			}
		}
	}
	document.getElementById(pref+'_'+postsel).checked=true;
/*	if(document.getElementById(pref+'_0').checked==true){
		alert('yes');
	}else alert('no');*/
}

function open_shipandbill(){
	if(EnableNext(['EMAIL','FNAME','LNAME','PHONE'],'NextPersonal',1)){
		close1(['tr2','tr1','tr4']);open1(['tr3']);
	}else{
		alert('It is necessary to fill a \'Personal Data\'!');
	}
}
function open_postalservices(){
	op_msg = '';
	if(!EnableNext(['EMAIL','FNAME','LNAME','PHONE'],'NextPersonal',1)){
		op_msg += 'It is necessary to fill a \'Personal Data\'!\n';
	}
	if(!EnableNext(['BILLADDRESS','BILLCITY','BILLSTATE','BILLCOUNTRY','BILLZIP','SHIPADDRESS','SHIPCITY','SHIPSTATE','SHIPCOUNTRY','SHIPZIP'],'NextBilling',1)){
		op_msg += 'It is necessary to fill a \'Shipping and Billing\' informations!';
	};
	
	if(op_msg != ''){
		alert(op_msg);
	}else{
		close1(['tr1','tr3','tr2']);open1(['tr4']);
	}
}

function go_update(){
	
}

function go_search(txt){
	document.getElementById('search_box').value=txt;
	document.getElementById('search_form').submit();
}

function country_change(sobj){
	obj = document.getElementById('t'+sobj+'COUNTRY');
	obj2 = document.getElementById('t'+sobj+'STATE');
	obj2.options.length = 0;
	obj2.options[obj2.options.length] = new Option(" - Select the state - ", "");
	if(CityToShop[obj.value]!=undefined){
		for (var cabbr in CityToShop[obj.value]){
			obj2.options[obj2.options.length] = new Option(CityToShop[obj.value][cabbr], cabbr);
		}
	}else{
		obj2.options[obj2.options.length] = new Option(CityToShop['XX']['XX'], 'XX');
	}
}
