arL = 0; parT = 0; parR = 0; parB = 0; isIE = null; dragEnabled = false; sizeEnabled = false; // globals
oneKg = 5;
var firstColourPrice = 0;
var nextColourPrice = 0;
shippingSetupCost = 0;

rate1 =12;
rate2 =20;

String.prototype.trim = function() {
	a = this.replace(/^\s+/, '');
	return a.replace(/\s+$/, '');
};
function onPageLoad() {
	// browser type
	if(document.all) document.getElementById("h_BrowserType").value = 'IE';
	document.getElementById("body_loading").style.display="none";
	// canvas offset X & Y
	if(typeof ltIE8 != 'undefined') {
		document.designer_form.method='POST';
		document.designer_form.enctype='multipart/form-data';
	}

//alert("test");

	var obj = document.getElementById("canvas_front"); // canvas DIV offset
	if (obj.offsetParent) {
		parL = obj.offsetLeft;
		parT = obj.offsetTop;
		while ((obj = obj.offsetParent)) {
			parL += obj.offsetLeft;
			parT += obj.offsetTop;
		}
//		parL += 12;
	}
	document.getElementById('h_CanvasOffsetX').value = parL;
	document.getElementById('h_CanvasOffsetY').value = parT;
//	alert(document.getElementById('h_CanvasOffsetY').value);
	parR = parL+document.getElementById("canvas_front").offsetWidth; // canvas DIV size
	parB = parT+document.getElementById("canvas_front").offsetHeight;

	// ilogo session cookie
	writeSessionCookie ("ilogoSID", document.getElementById('h_SessionID').value);
	//alert(getCookieValue("ilogoSID")); // OK, Works.
/*

	// position ui elements
	var uiElem = document.getElementById('priceTbox');
	if(uiElem) {
		uiElem.style.left = (parR - 86) + 'px';
		uiElem.style.top = (parT-30) + 'px';
		uiElem.style.display = 'block';
	}
	var uiElem = document.getElementById('priceTotal');
	if(uiElem) {
		uiElem.style.left = (parR - 137) + 'px';
		uiElem.style.top = (parT-25) + 'px';
		uiElem.style.display = 'block';
	}
	
	var uiElem = document.getElementById('priceAllInclusive');
	if(uiElem) {
		uiElem.style.left = (parR - 200) + 'px';
		uiElem.style.top = (parT-8) + 'px';
		uiElem.style.display = 'block';
	}
	
	var uiElem = document.getElementById('pricebox');
	if(uiElem) {
		uiElem.style.left = (parR - 186) + 'px';
		uiElem.style.top = (parT-30) + 'px';
		uiElem.style.display = 'block';
	}
	var uiElem = document.getElementById('priceUnit');
	if(uiElem) {
		uiElem.style.left = (parR - 335) + 'px';
		uiElem.style.top = (parT-25) + 'px';
		uiElem.style.display = 'block';
	}
	
	uiElem = document.getElementById('priceI');
	if(uiElem) {
		uiElem.style.left = (parR-11) + 'px';
		uiElem.style.top = (parT+4-30) + 'px';
		uiElem.style.display = 'block';
	}

	uiElem = document.getElementById('ProductList_XSELECT').parentNode;
	if(uiElem) {
		//uiElem.style.left = (parL-8) + 'px';
		//uiElem.style.top = (parT-30) + 'px';
		//uiElem.style.display = 'block';
		//uiElem.style.width='165px';
	}
*/

	var uiElem = document.getElementById('plswaitDIV');
	if(uiElem) {
		uiElem.style.left = ( parL + 20 ) + 'px';
		uiElem.style.top = ( parT + 40 ) + 'px';
	}
	
	// set part images
	var imgDir = document.getElementById('h_Location').value;
	imgDir = imgDir.replace("/var/www/vhosts/ilogo.in/subdomains/src1/httpdocs/","http://src1.ilogo.in/");

//alert(imgDir);
//	var bg = document.getElementById('imgBg');
	var brim = document.getElementById('imgBrim');
	var sleeves = document.getElementById('imgSleeves');
	var body = document.getElementById('imgBody');
	var btncuf = document.getElementById('imgBtncuf');
	var colext = document.getElementById('imgColext');
	if(document.all) { // IE
		//bg.src = 'images/spacer.png';
		brim.src = 'images/spacer.png';
		sleeves.src = 'images/spacer.png';
		body.src = 'images/spacer.png';
		btncuf.src = 'images/spacer.png';
		colext.src = 'images/spacer.png';
		//bg.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+(imgDir+"/front/bg.png?"+(new Date()).getTime())+"', sizingMethod='image');"
		brim.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+(imgDir+"/front/brim.png?"+(new Date()).getTime())+"', sizingMethod='image');"
		sleeves.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+(imgDir+"/front/sleeves.png?"+(new Date()).getTime())+"', sizingMethod='image');"
		body.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+(imgDir+"/front/body.png?"+(new Date()).getTime())+"', sizingMethod='image');"
		btncuf.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+(imgDir+"/front/btncuf.png?"+(new Date()).getTime())+"', sizingMethod='image');"
		colext.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+(imgDir+"/front/colext.png?"+(new Date()).getTime())+"', sizingMethod='image');"
	}
	else {
		//bg.src = (imgDir+"/front/bg.png?"+(new Date()).getTime());
		brim.src = (imgDir+"/front/brim.png?"+(new Date()).getTime());
		sleeves.src = (imgDir+"/front/sleeves.png?"+(new Date()).getTime());
		body.src = (imgDir+"/front/body.png?"+(new Date()).getTime());
		btncuf.src = (imgDir+"/front/btncuf.png?"+(new Date()).getTime());
		colext.src = (imgDir+"/front/colext.png?"+(new Date()).getTime());
	}
	
	if(!document.all) {
		brim.style.left = parL+'px'; brim.style.top = parT+'px';
		sleeves.style.left = parL+'px'; sleeves.style.top = parT+'px';
		body.style.left = parL+'px'; body.style.top = parT+'px';
		btncuf.style.left = parL+'px'; btncuf.style.top = parT+'px';
		colext.style.left = parL+'px'; colext.style.top = parT+'px';
	}

	brim.style.zIndex = 10;
	sleeves.style.zIndex = 11;
	body.style.zIndex = 12;
	btncuf.style.zIndex = 13;
	colext.style.zIndex = 14;
	
	brim.style.display = 'block';
	sleeves.style.display = 'block';
	body.style.display = 'block';
	btncuf.style.display = 'block';
	colext.style.display = 'block';
	addTotal(document);
	//countryChanged();
	doAjaxFetch_Weight();
	if(document.getElementById('IPCountryID') && document.getElementById('IPCountryName') && document.getElementById('IPCountryID').value !="" && document.getElementById('IPCountryName').value !="") {
		countrySelected("CountryList_XSELECT", document.getElementById('IPCountryID').value, document.getElementById('IPCountryName').value);
	}
}
// ITEM ID helpers
function createItemID(p_Face, p_Type, p_ID) {
	return 'DMIitem_'+p_Face+'_'+p_Type+'_'+p_ID;
}
function getItemFace(p_ItemID) {
	var parts = p_ItemID.split('_');
	return parts[1];
}
function getItemType(p_ItemID) {
	var parts = p_ItemID.split('_');
	return parts[2];
}
function getItemUniqueID(p_ItemID) {
	var parts = p_ItemID.split('_');
	return parts[3];
}
function isDesignItem(p_ElementID) {
	if(p_ElementID.indexOf('DMIitem_')==0) return true;
	return false;
}

// AJAX result handlers
function processPartsUpdate(p_Doc) {
	
	p_Doc.getElementById('brim_color_img').style.backgroundColor = p_Doc.getElementById('brim_color').value;
	p_Doc.getElementById('sleeves_color_img').style.backgroundColor = p_Doc.getElementById('sleeves_color').value;
	p_Doc.getElementById('body_color_img').style.backgroundColor = p_Doc.getElementById('body_color').value;
	p_Doc.getElementById('colext_color_img').style.backgroundColor = p_Doc.getElementById('colext_color').value;
	if(p_Doc.getElementById('btncuf_color'))
		p_Doc.getElementById('btncuf_color_img').style.backgroundColor = p_Doc.getElementById('btncuf_color').value;
	p_Doc.getElementById('colour_count').value=0;
	// set part images
	var imgDir = p_Doc.getElementById('h_Location').value;
	imgDir = imgDir.replace("/var/www/vhosts/ilogo.in/subdomains/src1/httpdocs/","http://src1.ilogo.in/");
//	alert(imgDir);
	//var bg = p_Doc.getElementById('imgBg');
	var brim = p_Doc.getElementById('imgBrim');
	var sleeves = p_Doc.getElementById('imgSleeves');
	var body = p_Doc.getElementById('imgBody');
	var btncuf = p_Doc.getElementById('imgBtncuf');
	var colext = p_Doc.getElementById('imgColext');
	if(p_Doc.all) { // IE
		//bg.src = 'images/spacer.png';
		brim.src = 'images/spacer.png';
		sleeves.src = 'images/spacer.png';
		body.src = 'images/spacer.png';
		btncuf.src = 'images/spacer.png';
		colext.src = 'images/spacer.png';
		//bg.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+(imgDir+"/front/bg.png?"+(new Date()).getTime())+"', sizingMethod='image');"
		brim.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+(imgDir+"/front/brim.png?"+(new Date()).getTime())+"', sizingMethod='image');"
		sleeves.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+(imgDir+"/front/sleeves.png?"+(new Date()).getTime())+"', sizingMethod='image');"
		body.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+(imgDir+"/front/body.png?"+(new Date()).getTime())+"', sizingMethod='image');"
		btncuf.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+(imgDir+"/front/btncuf.png?"+(new Date()).getTime())+"', sizingMethod='image');"
		colext.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+(imgDir+"/front/colext.png?"+(new Date()).getTime())+"', sizingMethod='image');"
	}
	else {
		//bg.src = (imgDir+"/front/bg.png?"+(new Date()).getTime());
		brim.src = (imgDir+"/front/brim.png?"+(new Date()).getTime());
		sleeves.src = (imgDir+"/front/sleeves.png?"+(new Date()).getTime());
		body.src = (imgDir+"/front/body.png?"+(new Date()).getTime());
		btncuf.src = (imgDir+"/front/btncuf.png?"+(new Date()).getTime());
		colext.src = (imgDir+"/front/colext.png?"+(new Date()).getTime());
	}
	
	/*
	 bg.style.left = parL+'px'; bg.style.top = parT+'px';
	brim.style.left = parL+'px'; brim.style.top = parT+'px';
	sleeves.style.left = parL+'px'; brim.style.top = parT+'px';
	body.style.left = parL+'px'; brim.style.top = parT+'px';
	colint.style.left = parL+'px'; brim.style.top = parT+'px';
	colext.style.left = parL+'px'; brim.style.top = parT+'px';
	*/
}
function processSinglePartUpdate(p_Doc, p_Result) {
	var result = p_Result.split(':::');
	if(result[0]!='OK') {
		alert(result[0]);
		return false;
	}
	
	
	var imgDir = p_Doc.getElementById('h_Location').value;
	imgDir = imgDir.replace("/var/www/vhosts/ilogo.in/subdomains/src1/httpdocs/","http://src1.ilogo.in/");
	//var bg = p_Doc.getElementById('imgBg');
	var part = p_Doc.getElementById('img'+result[2]);
	if(p_Doc.all) { // IE
		//bg.src = 'images/spacer.png';
		part.src = 'images/spacer.png';
		//bg.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+(imgDir+"/front/bg.png?"+(new Date()).getTime())+"', sizingMethod='image');"
		part.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+(imgDir+"/front/"+result[1]+".png?"+(new Date()).getTime())+"', sizingMethod='image');"
	}
	else {
		//alert(p_Result);
		//bg.src = (imgDir+"/front/bg.png?"+(new Date()).getTime());
		part.src = (imgDir+"/front/"+result[1]+".png?"+(new Date()).getTime());
	}
	var parts=new Array();
	parts[0]="brim_color";
	parts[1]="sleeves_color";
	parts[2]="body_color";
	parts[3]="colext_color";
	parts[4]="btncuf_color";
	var flag =0;
	var totalColours = 0; 
	for(i=0; i<5; i++){
		flag =0;
		for(j=i+1; j<5; j++){
			if(p_Doc.getElementById(parts[i]) && p_Doc.getElementById(parts[j])){
				if(p_Doc.getElementById(parts[i]).value == p_Doc.getElementById(parts[j]).value){
					flag =0;
					break;
				}
				flag =1;
			}
		}		
		totalColours += flag;    
	}
	
	p_Doc.getElementById('colour_count').value=totalColours;
	countryChanged_Ajax(p_Doc);
}
function processProductChanged(p_Doc, p_Result) {
	
	var result = p_Result.split(':::');
	if(result[0]!='OK') {
		alert(result[0]);
		return false;
	}
	p_Doc.getElementById('h_ProductID').value = result[1];
	
	// set part images
	var imgDir = p_Doc.getElementById('h_Location').value;
	imgDir = imgDir.replace("/var/www/vhosts/ilogo.in/subdomains/src1/httpdocs/","http://src1.ilogo.in/");
	//var bg = p_Doc.getElementById('imgBg');
	var brim = p_Doc.getElementById('imgBrim');
	var sleeves = p_Doc.getElementById('imgSleeves');
	var body = p_Doc.getElementById('imgBody');
	var btncuf = p_Doc.getElementById('imgBtncuf');
	var colext = p_Doc.getElementById('imgColext');
	if(document.all) { // IE
		//bg.src = 'images/spacer.png';
		brim.src = 'images/spacer.png';
		sleeves.src = 'images/spacer.png';
		body.src = 'images/spacer.png';
		btncuf.src = 'images/spacer.png';
		colext.src = 'images/spacer.png';
		//bg.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+(imgDir+"/front/bg.png?"+(new Date()).getTime())+"', sizingMethod='image');"
		brim.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+(imgDir+"/front/brim.png?"+(new Date()).getTime())+"', sizingMethod='image');"
		sleeves.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+(imgDir+"/front/sleeves.png?"+(new Date()).getTime())+"', sizingMethod='image');"
		body.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+(imgDir+"/front/body.png?"+(new Date()).getTime())+"', sizingMethod='image');"
		btncuf.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+(imgDir+"/front/btncuf.png?"+(new Date()).getTime())+"', sizingMethod='image');"
		colext.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+(imgDir+"/front/colext.png?"+(new Date()).getTime())+"', sizingMethod='image');"
	}
	else {
		//bg.src = (imgDir+"/front/bg.png?"+(new Date()).getTime());
		brim.src = (imgDir+"/front/brim.png?"+(new Date()).getTime());
		sleeves.src = (imgDir+"/front/sleeves.png?"+(new Date()).getTime());
		body.src = (imgDir+"/front/body.png?"+(new Date()).getTime());
		btncuf.src = (imgDir+"/front/btncuf.png?"+(new Date()).getTime());
		colext.src = (imgDir+"/front/colext.png?"+(new Date()).getTime());
	}
	
	p_Doc.getElementById('partsPanel').innerHTML = result[2];
	
				
//	p_Doc.getElementById('tdProductDetails').innerHTML = result[3];
	p_Doc.getElementById('tdQuantity').innerHTML = result[4];
	p_Doc.getElementById('pricebox').innerHTML = result[5];
	p_Doc.getElementById('h_Price').value = result[5];
	p_Doc.getElementById('partsColours').innerHTML = result[6];
	p_Doc.getElementById('productMeterials').innerHTML = result[7];
	window.parent.setupXSelect('MaterialList');
	addTotal(p_Doc);
	countryChanged_Ajax(p_Doc);
	
	// reset parts pane
	/*p_Doc.getElementById('body_color').value = p_Doc.getElementById('body_color_img').style.backgroundColor = '#ffffff';
	p_Doc.getElementById('brim_color').value = p_Doc.getElementById('brim_color_img').style.backgroundColor = '#ffffff';
	p_Doc.getElementById('sleeves_color').value = p_Doc.getElementById('sleeves_color_img').style.backgroundColor = '#ffffff';
	p_Doc.getElementById('btncuf_color').value = p_Doc.getElementById('btncuf_color_img').style.backgroundColor = '#ffffff';
	p_Doc.getElementById('colext_color').value = p_Doc.getElementById('colext_color_img').style.backgroundColor = '#ffffff';*/
	
}
function processBuyNowResult(p_Doc, p_Result) {
	var result = p_Result.split(':::');
	if(result[0]!='OK') {
		alert(result[0]);	
		return false;
	}
	var sid = p_Doc.getElementById('h_SessionID').value;
	var cartUrl = 'cart.php?s='+sid;
	window.parent.location.replace(cartUrl);
	return true;
}
function processCommandResult(p_SID, p_Cmd, p_Doc, p_ResultDoc) {
	
	p_Doc.getElementById('requestPending').value = 0;
	
	// validate sid
	if(p_Doc.getElementById('h_SessionID').value!=p_SID) {
		alert('Session corrupt!');
		return;
	}

	// fetch the result string
	var result = p_ResultDoc.getElementById('cmdResult').innerHTML;
	
	// process the command result
	switch(p_Cmd) {
		
		case 'updateparts':
			processPartsUpdate(p_Doc);
			break;
		
		case 'updateparts_body_color':
		case 'updateparts_brim_color':
		case 'updateparts_sleeves_color':
		case 'updateparts_colext_color':
		case 'updateparts_btncuf_color':
			processSinglePartUpdate(p_Doc, result);
			break;
		
		case 'prodchange':
			processProductChanged(p_Doc, result);
			break;
		
		case 'order': processBuyNowResult(p_Doc, result); break;
		case 'addtocart': showCartPopup(p_Doc); break;
		case 'save': processSaveResult(p_Doc, result); break;
	}

//	showLoadingIndicator(p_Doc, false);

	p_Doc.getElementById('plswaitDIV').style.display='none';
	
}
function localValidate(p_Cmd) {
	switch(p_Cmd) {
		case 'order':
		case 'addtocart':
			if(document.getElementById('h_DesignID').value==0) {
				//if(confirm('You have to save your product before it can be added to the shopping cart. \nDo you want to save it now?')) {
					showSavePopup();
				//}
				return false;
			}
			return true;
			break;
		
		case 'updateparts_body_color':
		case 'updateparts_brim_color':
		case 'updateparts_sleeves_color':
		case 'updateparts_colext_color':
		case 'updateparts_btncuf_color':
			document.getElementById('requestPending').value = 1;
			break;
		
	
	}
	return true;
}
function isCountrySelected_Submit(p_Doc) {
	
	if(p_Doc.getElementById('CountryList').value==0) {
		alert('You have to select the country of delivery before you can save the T-shirt.');
		return false;
	}
	
	return true;
}
function submitButtonClicked(p_Command) {
	if(p_Command == 'order' || p_Command == 'addtocart' || p_Command == 'save'){
		if(!isCountrySelected_Submit(document)){
			return false;
		}
/*
	var price1 = parseFloat(document.getElementById('h_Price').value);
//		var itemCount = parseInt(document.getElementById('colour_count').value);
//		price1 += (itemCount*nextColourPrice);
		document.getElementById('h_Total_Amount').value = price1;
*/
		var rate = parseFloat(document.getElementById('h_Country_Rate').value);
		var curencyRate = parseFloat(document.getElementById('h_Currency_Rate').value);
		
		var price1 = ((parseFloat(document.getElementById('pricebox').innerHTML)*100)/(rate*100));
		price1 = ((price1*100)/(curencyRate*100));
		document.getElementById('h_Total_Amount').value = price1;
	}
	//alert(document.getElementById('h_Total_Price').value);
	if(p_Command == 'order' || p_Command == 'addtocart'){
		if(isQuantitySpecified(document, 100)){
			if(localValidate(p_Command)) {
				//showLoadingIndicator(p_Doc, true);
				document.getElementById('plswaitDIV').style.display='block';
				document.getElementById('h_Command').value = p_Command;
				document.designer_form.submit();
			}
		}			
	}
	else{
		if(localValidate(p_Command)) {
			//showLoadingIndicator(p_Doc, true);
			document.getElementById('plswaitDIV').style.display='block';
			document.getElementById('h_Command').value = p_Command;
			document.designer_form.submit();
		}
	}
}

function isQuantitySpecified(p_Doc, minQty) {
	addTotal(p_Doc);
	var totQty = parseInt(p_Doc.getElementById('qty_total').innerHTML);
	
	if(!totQty || totQty==0) {
		alert('You have to specify the desired quantity before you can customize the T-shirt.');
		if(p_Doc.getElementById('qty_S')) p_Doc.getElementById('qty_S').focus();
		else p_Doc.getElementById('qty_M').focus();
		return false;
	}
	else if(minQty>totQty){
		alert('You have to specify the minimum '+ minQty+' t-shirts before you can proceed.');
		if(p_Doc.getElementById('qty_S')) p_Doc.getElementById('qty_S').focus();
		else p_Doc.getElementById('qty_M').focus();
		return false;
	}
	return true;
}

function sideChanged(p_SideIndex) {
	var sideName = 'front';

	switch(p_SideIndex) {
		case 0: sideName = 'front'; break;
		case 1: sideName = 'frontr'; break;
		case 2: sideName = 'frontl'; break;
		case 3: sideName = 'back'; break;
	}
	
	var face = document.getElementById('h_CurrFace').value;
	document.getElementById('canvas_'+face).style.display='none';
	document.getElementById('canvas_'+sideName).style.display='block';
	document.getElementById('h_CurrFace').value = sideName;
	//setCurrentImage(document, '-1', document.getElementById('h_CurrImageType').value);
	setCurrentItem(document, '');
}



function setFace(p_Doc, p_Face) {
	var face = p_Doc.getElementById('h_CurrFace').value;
	if(face==p_Face) return;
	p_Doc.getElementById('canvas_'+face).style.display='none';
	p_Doc.getElementById('canvas_'+p_Face).style.display='block';
	p_Doc.getElementById('h_CurrFace').value = p_Face;
	setCurrentImage(p_Doc, '');
}

function addTotal(p_Doc){
	var val0 = '0';
	if(p_Doc.getElementById('qty_S')) val0=(p_Doc.getElementById('qty_S').value).trim();
	
	var val1=(p_Doc.getElementById('qty_M').value).trim();
	var val2=(p_Doc.getElementById('qty_L').value).trim();
	var val3=(p_Doc.getElementById('qty_XL').value).trim();
	var val4=(p_Doc.getElementById('qty_XXL').value).trim();
	
	var val5 = '0';
	if(p_Doc.getElementById('qty_XXXL')) val5=(p_Doc.getElementById('qty_XXXL').value).trim();
	
	if(val0=='') val0='0';
	if(val1=='') val1='0';
	if(val2=='') val2='0';
	if(val3=='') val3='0';
	if(val4=='') val4='0';
	if(val5=='') val5='0';
	
	while(val0.indexOf('0')==0 && val0.length>1) {
		val0=val0.substring(1);
	}
	
	while(val1.indexOf('0')==0 && val1.length>1) {
		val1=val1.substring(1);
	}
	
	while(val2.indexOf('0')==0 && val2.length>1) {
		val2=val2.substring(1);
	}
	
	while(val3.indexOf('0')==0 && val3.length>1) {
		val3=val3.substring(1);
	}
	
	while(val4.indexOf('0')==0 && val4.length>1) {
		val4=val4.substring(1);
	}
	
	while(val5.indexOf('0')==0 && val5.length>1) {
		val5=val5.substring(1);
	}
	
	if(!parseInt(val0)) val0='0';
	if(!parseInt(val1)) val1='0';
	if(!parseInt(val2)) val2='0';
	if(!parseInt(val3)) val3='0';
	if(!parseInt(val4)) val4='0';
	if(!parseInt(val5)) val5='0';
	
	
	if(p_Doc.getElementById('qty_S')) p_Doc.getElementById('qty_S').value=val0;
	p_Doc.getElementById('qty_M').value=val1;
	p_Doc.getElementById('qty_L').value=val2;
	p_Doc.getElementById('qty_XL').value=val3;
	p_Doc.getElementById('qty_XXL').value=val4;
	if(p_Doc.getElementById('qty_XXXL')) p_Doc.getElementById('qty_XXXL').value=val5;

	var v0=parseInt(val0);
	var v1=parseInt(val1);
	var v2=parseInt(val2);
	var v3=parseInt(val3);
	var v4=parseInt(val4);
	var v5=parseInt(val5);
	p_Doc.getElementById('qty_total').innerHTML=""+(v0+v1+v2+v3+v4+v5);
	
	addTotalPrice(p_Doc);
}
function addTotalPrice(p_Doc){
	var currencyID = 'h_Currency_'+p_Doc.getElementById('CountryList').value;
	var currency = p_Doc.getElementById(currencyID).innerHTML;
	p_Doc.getElementById('h_Currency').value = currency;
	
	var total = parseFloat(p_Doc.getElementById('pricebox').innerHTML);
	var priceformat = total.toFixed(2)+ '<span class="CURRENCY">'+currency+'</span>';
	p_Doc.getElementById('priceTbox').innerHTML = priceformat;
/*
	var total = ''+ parseFloat(p_Doc.getElementById('pricebox').innerHTML)*parseInt(p_Doc.getElementById('qty_total').innerHTML);
	p_Doc.getElementById('priceTbox').innerHTML = Math.round(total*100)/100 + '<span class="CURRENCY">'+currency+'</span>';
*/	
}
function replaceAll(instr, key, repl) {
	var result = instr;
	while(result.indexOf(key)>=0) {
		result=result.replace(key, repl);
	}
	return result;
}
function calculatePrice(){
	var dropdown = document.getElementById('CountryList');
	var myindex  = dropdown.selectedIndex;
	var rate = parseInt(dropdown.options[myindex].value);
	var price1 = parseInt(document.getElementById('h_Price').value);
//	var total = price1+((price1*rate)/100);
	var total = price1;
	if (total == 'NaN') total=0.0;
	document.getElementById('pricebox').innerHTML = total; //"&euro; "+total;
}
function showNamesPopup() {
	addTotal();
	var totQty = parseInt(document.getElementById('qty_total').innerHTML);
	var val_M=parseInt((document.getElementById('qty_M').value).trim());
	var val_L=parseInt((document.getElementById('qty_L').value).trim());
	var val_XL=parseInt((document.getElementById('qty_XL').value).trim());
	var val_XXL=parseInt((document.getElementById('qty_XXL').value).trim());
	
	
	if(!totQty || totQty==0) {
		alert('Please specify the number of shirts you want to order before assigning names/numbers');
		document.getElementById('qty_M').focus();
		return;
	}
	var popup = document.getElementById('namesPopup');
	//var popupBG = document.getElementById('namesPopupBG');
	popup.style.left = document.getElementById('h_CanvasOffsetX').value + 'px';
	popup.style.top = document.getElementById('h_CanvasOffsetY').value + 'px';
	//popupBG.style.left = document.getElementById('h_CanvasOffsetX').value + 'px';
	//popupBG.style.top = document.getElementById('h_CanvasOffsetY').value + 'px';
	var face = document.getElementById('h_CurrFace').value;
	popup.style.width = document.getElementById('canvas_'+face).offsetWidth + 'px';
	popup.style.height = ( document.getElementById('canvas_'+face).offsetHeight + 15 ) + 'px';
	//popupBG.style.width = document.getElementById('canvas_'+face).offsetWidth + 'px';
	//popupBG.style.height = ( document.getElementById('canvas_'+face).offsetHeight + 15 ) + 'px';
	
	var html = '<div style="width:100%; height:360px; overflow: auto;" align="center">';
	html += '<table cellspacing="5" cellpadding="0" border="0" width="100%">';
	html += '<tr>'+
				'<td class="SECTIONHEAD" width="20%">Number</td>'+
				'<td class="SECTIONHEAD" width="70%">Name</td>'+
				'<td class="SECTIONHEAD" width="10%" align="center">Size</td>'+
			'</tr>';
	var i=0;
	
	for(i=0; i<val_M; i++) {
		html += 
			'<tr>'+
				'<td class="SECTIONDATA" ><input type="text" style="width:100%;" ';
				if(!document.getElementById('inTeam_NumReqd').checked) html+= 'DISABLED';
				html+= '></td>'+
				
				'<td class="SECTIONDATA" ><input type="text" style="width:100%;" ';
				if(!document.getElementById('inTeam_NameReqd').checked) html+= 'DISABLED';		
				html+= '></td>'+
				
				'<td class="SECTIONDATA" align="left">M</td>'+
			'</tr>';
	}
	for(i=0; i<val_L; i++) {
		html += 
			'<tr>'+
				'<td class="SECTIONDATA" ><input type="text" style="width:100%;" ';
		if(!document.getElementById('inTeam_NumReqd').checked) html+= 'DISABLED';
		html+= '></td>'+
				'<td class="SECTIONDATA" ><input type="text" style="width:100%;" ';
		if(!document.getElementById('inTeam_NameReqd').checked) html+= 'DISABLED';		
		html+= '></td>'+
				'<td class="SECTIONDATA" align="left">L</td>'+
			'</tr>';
	}
	for(i=0; i<val_XL; i++) {
		html += 
			'<tr>'+
				'<td class="SECTIONDATA" ><input type="text" style="width:100%;" ';
		if(!document.getElementById('inTeam_NumReqd').checked) html+= 'DISABLED';
		html+= '></td>'+
				'<td class="SECTIONDATA" ><input type="text" style="width:100%;" ';
		if(!document.getElementById('inTeam_NameReqd').checked) html+= 'DISABLED';		
		html+= '></td>'+
				'<td class="SECTIONDATA" align="left">XL</td>'+
			'</tr>';
	}
	for(i=0; i<val_XXL; i++) {
		html += 
			'<tr>'+
				'<td class="SECTIONDATA" ><input type="text" style="width:100%;" ';
		if(!document.getElementById('inTeam_NumReqd').checked) html+= 'DISABLED';
		html+= '></td>'+
				'<td class="SECTIONDATA" ><input type="text" style="width:100%;" ';
		if(!document.getElementById('inTeam_NameReqd').checked) html+= 'DISABLED';		
		html+= '></td>'+
				'<td class="SECTIONDATA" align="left">XXL</td>'+
			'</tr>';
	}


	html += '</table></div><br><img style="cursor: pointer;" src="images/savenames.gif" onclick="hideNamesPopup();">&nbsp;&nbsp;<img style="cursor: pointer;" src="images/cancelnames.gif" onclick="hideNamesPopup();">';
	document.getElementById('namesPopupContent').innerHTML = html;
	
	popup.style.display = 'block';
	//popupBG.style.display = 'block';
}
function hideNamesPopup() {
	var popup = document.getElementById('namesPopup');
	popup.style.display = 'none';
}
function showClipsPopup() {
	var popup = document.getElementById('clipsPopup');
	popup.style.left = document.getElementById('h_CanvasOffsetX').value + 'px';
	popup.style.top = document.getElementById('h_CanvasOffsetY').value + 'px';
	var face = document.getElementById('h_CurrFace').value;
	popup.style.width = document.getElementById('canvas_'+face).offsetWidth + 'px';
	popup.style.height = ( document.getElementById('canvas_'+face).offsetHeight + 15 ) + 'px';
	var html = '<div style="width:100%; height:360px; overflow: auto;" align="center"><table cellspacing="5" cellpadding="0" border="0" width="100%">';
	html += '<tr><td class="SECTIONHEAD" width="20%">Number</td><td class="SECTIONHEAD" width="60%">Name</td><td class="SECTIONHEAD" width="20%" align="center">Size</td></tr>';
	html += '</table></div><br><img style="cursor: pointer;" src="images/chooseclip.gif" onclick="hideClipsPopup();">';
	document.getElementById('clipsPopupContent').innerHTML = '';
	popup.style.display = 'block';
}
function hideClipsPopup() {
	var popup = document.getElementById('clipsPopup');
	popup.style.display = 'none';
}
function showSavePopup() {
	if(!isCountrySelected_Submit(document)){
		return false;
	}
	if(!isQuantitySpecified(document, 100)){
		return false;
	}
	var popup = document.getElementById('savePopup');
	popup.style.left = document.getElementById('h_CanvasOffsetX').value + 'px';
	popup.style.top = (parseInt(document.getElementById('h_CanvasOffsetY').value) + 100) + 'px';
//	var face = document.getElementById('h_CurrFace').value;
//	popup.style.width = document.getElementById('canvas_'+face).offsetWidth + 'px';
//	popup.style.height = ( document.getElementById('canvas_'+face).offsetHeight + 15 ) + 'px';
	popup.style.display = 'block';
}
function hideSavePopup() {
	var popup = document.getElementById('savePopup');
	popup.style.display = 'none';
}
function showCartPopup(p_Doc) {
	var popup = p_Doc.getElementById('cartPopup');
	popup.style.left = p_Doc.getElementById('h_CanvasOffsetX').value + 'px';
	popup.style.top = (parseInt(p_Doc.getElementById('h_CanvasOffsetY').value) + 100) + 'px';
//	var face = p_Doc.getElementById('h_CurrFace').value;
//	popup.style.width = p_Doc.getElementById('canvas_'+face).offsetWidth + 'px';
//	popup.style.height = ( document.getElementById('canvas_'+face).offsetHeight + 15 ) + 'px';
	popup.style.display = 'block';
}
function hideCartPopup() {
	var popup = document.getElementById('cartPopup');
	popup.style.display = 'none';
}
function showSavedPopup(p_Doc) {
	isCountrySelected_Submit(p_Doc);
	var popup = p_Doc.getElementById('savedPopup');
	popup.style.left = p_Doc.getElementById('h_CanvasOffsetX').value + 'px';
	popup.style.top = (parseInt(p_Doc.getElementById('h_CanvasOffsetY').value) + 100) + 'px';
//	var face = p_Doc.getElementById('h_CurrFace').value;
//	popup.style.width = p_Doc.getElementById('canvas_'+face).offsetWidth + 'px';
//	popup.style.height = ( document.getElementById('canvas_'+face).offsetHeight + 15 ) + 'px';
	popup.style.display = 'block';
}
function hideSavedPopup() {
	var popup = document.getElementById('savedPopup');
	popup.style.display = 'none';
}
function validateName() {
	var dName = document.getElementById('designName');
	if(dName.value.trim() == '') {
		alert('Please enter a valid name for the design.');
		dName.focus();
		return false;
	}
	if(document.getElementById('designEmail').value.trim() != ''){
		if(!checkEmail(document.getElementById('designEmail'))){
			document.getElementById('designEmail').focus();
			return false;
		}
	}
	if(document.getElementById('email1').value.trim() != ''){
		if(!checkEmail(document.getElementById('email1'))){
			document.getElementById('email1').focus();
			return false;
		}
	}
	if(document.getElementById('email2').value.trim() != ''){
		if(!checkEmail(document.getElementById('email2'))){
			document.getElementById('email2').focus();
			return false;
		}
	}
	if(document.getElementById('email3').value.trim() != ''){
		if(!checkEmail(document.getElementById('email3'))){
			document.getElementById('email3').focus();
			return false;
		}
	}
	if(document.getElementById('email4').value.trim() != ''){
		if(!checkEmail(document.getElementById('email4'))){
			document.getElementById('email4').focus();
			return false;
		}
	}
	if(document.getElementById('email5').value.trim() != ''){
		if(!checkEmail(document.getElementById('email5'))){
			document.getElementById('email5').focus();
			return false;
		}
	}
	return true;
}

function checkEmail(myForm) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.value)){
	return (true)
	}
	alert("Invalid E-mail Address! Please re-enter.")
	return (false)
}

function processSaveResult(p_Doc, temp) {
	var result = temp.split(':::');
	if(result[0]!='OK') {
		alert(result[0]);
		return false;
	}
	p_Doc.getElementById('h_DesignID').value = result[1];
	showSavedPopup(p_Doc);
	return true;
}
function onProductTypeChnage(){
	countryChanged();
}
function countryChanged() {
	var num = document.getElementById('CountryList').value
	//Country rate
	var rate = document.getElementById('h_RateForCountry_'+num).innerHTML;
	document.getElementById('h_Country_Rate').value = rate;

	//Custom & VAT rate
	var rate1 = document.getElementById('h_RateForCountryCustom_'+num).innerHTML;
	var rate2 = document.getElementById('h_RateForCountryVat_'+num).innerHTML;
	
	//Currency rate
	var curencyRate = document.getElementById('h_RateOfCurrency_'+num).innerHTML;
	document.getElementById('h_Currency_Rate').value = curencyRate;
	
	//Zone code
	var countryZone = document.getElementById('h_CountryZone_'+num).innerHTML;
	document.getElementById('h_Zone').value = countryZone;
	
	var totalQty = parseInt(document.getElementById('qty_total').innerHTML);
	var meterialID = document.getElementById('MaterialList').value;
	
	if(totalQty <= 500){
		firstColourPrice = document.getElementById('h_colorl_500_'+meterialID).value;
		nextColourPrice	 = document.getElementById('h_nextcolorl_500_'+meterialID).value;
	} else if(totalQty <= 1000){
		firstColourPrice = document.getElementById('h_colorg_500_'+meterialID).value;
		nextColourPrice	 = document.getElementById('h_nextcolorg_500_'+meterialID).value;
	} else if(totalQty > 1000){
		firstColourPrice = document.getElementById('h_colorg_1000_'+meterialID).value;
		nextColourPrice	 = document.getElementById('h_nextcolorg_1000_'+meterialID).value;
	}

	//document.getElementById('h_Price').value = firstColourPrice;

	// Base Price	
	var price1 = parseFloat(firstColourPrice);
	var itemCount = parseInt(document.getElementById('colour_count').value);
	price1 += (itemCount*nextColourPrice);
	document.getElementById('h_Price').value = price1;

	// Add Custom duty and VAT Price	
	var Rate  = parseFloat(rate1)+parseFloat(rate2);
	price1 += (price1*Rate)/100;

	// Add shipping cost 
	price1 += shippingSetupCost/totalQty;
	
	// Add weight rate
	if(document.getElementById('h_AjaxRequestStatus').value == 0)
		pre_doAjaxFetch_Weight(document);
	var weight_rate = parseFloat(document.getElementById('h_Product_Weight_Rate').value)/totalQty;
	document.getElementById('h_AjaxRequestStatus').value =0;
	price1 +=weight_rate;

	price1 =(price1*rate); // adding Country rate
	var total = price1*curencyRate;
	document.getElementById('pricebox').innerHTML = total.toFixed(2);// Currency rate factor
	addTotalPrice(document);
}

function countryChanged_Ajax(p_Doc) {
	var num = p_Doc.getElementById('CountryList').value;

	var rate = p_Doc.getElementById('h_RateForCountry_'+num).innerHTML;
	p_Doc.getElementById('h_Country_Rate').value = rate;
	
	var rate1 = p_Doc.getElementById('h_RateForCountryCustom_'+num).innerHTML;
	
	var rate2 = p_Doc.getElementById('h_RateForCountryVat_'+num).innerHTML;

	var curencyRate = p_Doc.getElementById('h_RateOfCurrency_'+num).innerHTML;
	
	var totalQty = parseInt(p_Doc.getElementById('qty_total').innerHTML);
	var meterialID = p_Doc.getElementById('MaterialList').value;
	
	var countryZone = p_Doc.getElementById('h_CountryZone_'+num).innerHTML;
	
	p_Doc.getElementById('h_Zone').value = countryZone;
	//var zoneCharge  = p_Doc.getElementById('h_Zone_'+countryZone).value;
	
	if(totalQty <= 500){
		firstColourPrice = p_Doc.getElementById('h_colorl_500_'+meterialID).value;
		nextColourPrice	 = p_Doc.getElementById('h_nextcolorl_500_'+meterialID).value;
	}
	else if(totalQty <= 1000){
		firstColourPrice = p_Doc.getElementById('h_colorg_500_'+meterialID).value;
		nextColourPrice	 = p_Doc.getElementById('h_nextcolorg_500_'+meterialID).value;
	}
	else if(totalQty > 1000){
		firstColourPrice = p_Doc.getElementById('h_colorg_1000_'+meterialID).value;
		nextColourPrice	 = p_Doc.getElementById('h_nextcolorg_1000_'+meterialID).value;
	}
	
	// Base Price	
	var price1 = parseFloat(firstColourPrice);
	var itemCount = parseInt(p_Doc.getElementById('colour_count').value);
	price1 += (itemCount*nextColourPrice);
	p_Doc.getElementById('h_Price').value = price1;

	// Add Custom duty and VAT Price	
	var Rate  = parseFloat(rate1)+parseFloat(rate2);
	price1 += (price1*Rate)/100;

	// Add shipping cost
	price1 += shippingSetupCost/totalQty;
	
	// Add weight rate
	if(p_Doc.getElementById('h_AjaxRequestStatus').value == 0)
		pre_doAjaxFetch_Weight(p_Doc);
	var weight_rate = parseFloat(p_Doc.getElementById('h_Product_Weight_Rate').value)/totalQty;
	p_Doc.getElementById('h_AjaxRequestStatus').value =0;
	price1 +=weight_rate;

	price1 =(price1*rate); // adding Country rate
	var total = price1*curencyRate;
	p_Doc.getElementById('pricebox').innerHTML = total.toFixed(2);// Currency rate factor
	addTotalPrice(p_Doc);
}
function pre_countryChanged_Ajax(p_Doc) {
	var num = p_Doc.getElementById('CountryList').value;
	var rate = p_Doc.getElementById('h_RateForCountry_'+num).innerHTML;

	var rate1 = p_Doc.getElementById('h_RateForCountryCustom_'+num).innerHTML;
	
	var rate2 = p_Doc.getElementById('h_RateForCountryVat_'+num).innerHTML;
	
	var curencyRate = p_Doc.getElementById('h_RateOfCurrency_'+num).innerHTML;
	
	var totalQty = parseInt(p_Doc.getElementById('qty_total').innerHTML);
	var meterialID = p_Doc.getElementById('MaterialList').value;
	
	var countryZone = p_Doc.getElementById('h_CountryZone_'+num).innerHTML;
	p_Doc.getElementById('h_Zone').value = countryZone;
	//var zoneCharge  = p_Doc.getElementById('h_Zone_'+countryZone).value;
	
	if(totalQty <= 500){
		firstColourPrice = p_Doc.getElementById('h_colorl_500_'+meterialID).value;
		nextColourPrice	 = p_Doc.getElementById('h_nextcolorl_500_'+meterialID).value;
	}
	else if(totalQty <= 1000){
		firstColourPrice = p_Doc.getElementById('h_colorg_500_'+meterialID).value;
		nextColourPrice	 = p_Doc.getElementById('h_nextcolorg_500_'+meterialID).value;
	}
	else if(totalQty > 1000){
		firstColourPrice = p_Doc.getElementById('h_colorg_1000_'+meterialID).value;
		nextColourPrice	 = p_Doc.getElementById('h_nextcolorg_1000_'+meterialID).value;
	}
	
	// Base Price	
	var price1 = parseFloat(firstColourPrice);
	var itemCount = parseInt(p_Doc.getElementById('colour_count').value);
	price1 += (itemCount*nextColourPrice);
	p_Doc.getElementById('h_Price').value = price1;

	// Add Custom duty and VAT Price	
	var Rate  = parseFloat(rate1)+parseFloat(rate2);
	price1 += (price1*Rate)/100;

	// Add shipping cost
	price1 += shippingSetupCost/totalQty;
	
	// Add weight rate
	var weight_rate = parseFloat(p_Doc.getElementById('h_Product_Weight_Rate').value)/totalQty;
	p_Doc.getElementById('h_AjaxRequestStatus').value =0;
	price1 +=weight_rate;

	price1 =(price1*rate); // adding Country rate
	var total = price1*curencyRate;
	p_Doc.getElementById('pricebox').innerHTML = total.toFixed(2);// Currency rate factor
	addTotalPrice(p_Doc);
}

function showCountryPopup(){
	
	var popup = document.getElementById('CountryPopup');
	var face =  document.getElementById('h_CurrFace').value;
	popup.style.left = document.getElementById('h_CanvasOffsetX').value + 'px';
	popup.style.top = document.getElementById('h_CanvasOffsetY').value + 'px';
	
	popup.style.width = document.getElementById('canvas_'+face).offsetWidth + 'px';
	
	popup.style.height = ( document.getElementById('canvas_'+face).offsetHeight + 15 ) + 'px';
	popup.style.display = 'block';
}
function hideCountryPopup(){
	var popup = document.getElementById('CountryPopup');
	popup.style.display = 'none';
}
function onCountryChange(id, name){
	document.getElementById('CountryList').value = id;
	
	//document.getElementById('CountryList_img').innerHTML = name;
	countryChanged();
	hideCountryPopup();
}
function colorChanged() {
	document.getElementById('qty_M').focus();
}

function paneActivated(p_Doc, p_PaneID) {
	p_Doc.getElementById('textPanel').style.display = 'none';
	p_Doc.getElementById('picturePanel').style.display = 'none';
	p_Doc.getElementById('teamPanel').style.display = 'none';
	p_Doc.getElementById('fontsPanel').style.display = 'none';
	p_Doc.getElementById('colorsPanel').style.display = 'none';
	p_Doc.getElementById(p_PaneID).style.display = 'inline';
	
	p_Doc.getElementById('tab_textPanel').src = 'images/text0.gif';
	p_Doc.getElementById('tab_picturePanel').src = 'images/pic0.gif';
	p_Doc.getElementById('tab_teamPanel').src = 'images/team0.gif';
	
	switch(p_PaneID) {
	case 'textPanel': p_Doc.getElementById('tab_textPanel').src = 'images/text1.gif'; break;
	case 'picturePanel': p_Doc.getElementById('tab_picturePanel').src = 'images/pic1.gif'; break;
	case 'teamPanel': p_Doc.getElementById('tab_teamPanel').src = 'images/team1.gif'; break;
	}
	
	p_Doc.getElementById('currentPane').value = p_PaneID;
}

function showColorsPane(p_FieldID) {
	document.getElementById('partsPanel').style.display = 'none';
	document.getElementById('colorsPanel').style.display = 'inline';
	document.getElementById('colorFieldID').value = p_FieldID;
	
}
function showCountryPane(p_FieldID) {
	document.getElementById('partsPanel').style.display = 'none';
	document.getElementById('colorsPanel').style.display = 'inline';
	document.getElementById('colorFieldID').value = p_FieldID;
	
}
function discardColorsPane(p_SelectedColor) {
	document.getElementById('colorsPanel').style.display = 'none';
	document.getElementById('partsPanel').style.display = 'inline';
	if(document.getElementById('requestPending').value == 1) {
		alert('Please wait. The last color change request is being processed!');
		return;
	}
	if(p_SelectedColor!='') {
		var colorField = document.getElementById('colorFieldID').value;
		//alert(colorField);
		document.getElementById(colorField+'_img').style.backgroundColor = p_SelectedColor;
		document.getElementById(colorField).value = p_SelectedColor;
		if(document.getElementById('colorFieldID').value == 'base_colour'){
			document.getElementById('brim_color').value = p_SelectedColor;
			document.getElementById('sleeves_color').value = p_SelectedColor;
			document.getElementById('body_color').value = p_SelectedColor;
			if(document.getElementById('btncuf_color'))
				document.getElementById('btncuf_color').value = p_SelectedColor;
			document.getElementById('colext_color').value = p_SelectedColor;
		
			submitButtonClicked('updateparts');
		}
		else{
			submitButtonClicked('updateparts_'+colorField);
		}
	}
}

function changeColor(p_Color) {
	var cFieldID = document.getElementById('currentColorField').value;
	var cField = document.getElementById(cFieldID);
	if(cField) {
		cField.value = p_Color;
		var cFieldImg = document.getElementById(cFieldID+'_img');
		if(cFieldImg) {
			cFieldImg.style.backgroundColor = p_Color;
		}
	}
}
function leftAlign() {
	var item = document.getElementById('h_CurrImageID').value;
	item = document.getElementById(item);
	if(item) {
		var face = document.getElementById('h_CurrFace').value;
		var bL = parseInt(document.getElementById('h_CanvasOffsetX').value);
		item.style.left = bL+'px';
	}
}
function centerAlign() {
	var item = document.getElementById('h_CurrImageID').value;
	item = document.getElementById(item);
	if(item) {
		var face = document.getElementById('h_CurrFace').value;
		var bL = parseInt(document.getElementById('h_CanvasOffsetX').value);
		var cW = parseInt(document.getElementById('canvas_'+face).offsetWidth);
		item.style.left = (bL+(cW/2-parseInt(item.offsetWidth)/2))+'px';
	}
}
function rightAlign() {
	var item = document.getElementById('h_CurrImageID').value;
	item = document.getElementById(item);
	if(item) {
		var face = document.getElementById('h_CurrFace').value;
		var bL = parseInt(document.getElementById('h_CanvasOffsetX').value);
		var cW = parseInt(document.getElementById('canvas_'+face).offsetWidth);
		item.style.left = (bL+(cW-parseInt(item.offsetWidth)))+'px';
	}
}
function changefield(val,type){
	//if(val!=0) window.location="create.php?p="+val;
	submitButtonClicked('prodchange');
}
function gotoDesigner() {
	if(document.getElementById('h_DesignID').value==0) {
		if(!isCountrySelected_Submit(document)){
			return false;
		}
		//if(confirm('You have to save your product before it can be customized using the designer. \nDo you want to save it now?')) {
			showSavePopup();
		//}
		return false;
	}
	var designURL = 'design.php?p='+document.getElementById('h_DesignID').value;
	window.parent.location.replace(designURL);
	return true;
}
function printDesign() {
	if(document.getElementById('h_DesignID').value==0) {
		if(confirm('You have to save your product before it can be printed. \nDo you want to save it now?')) {
			showSavePopup();
		}
		return;
	}
	var url = 'pprint.php?p='+document.getElementById('h_DesignID').value;
	var features = 'status=1,toolbar=0,resizable=1,menubar=0,scrollbars=1,width=720,height=600';
	window.open(url, 'printWindow', features);
}
/*
//old
function issueProductChange() {
	var productID = document.getElementById('ProductList').value
	if(productID == '')
		return false;
	document.getElementById('h_ProductID').value = productID;
	//resetProductImageSizes();
	submitButtonClicked('prodchange');
}
*/
function issueProductChange(id) {
	var productID = id;
	if(productID == '')
		return false;
	document.getElementById("cate_opt").style.display = "none";
	document.getElementById("default_opt").style.display = "block";

	document.getElementById('h_ProductID').value = productID;
	document.getElementById('ProductList').value = productID;
	submitButtonClicked('prodchange');
}



/********************************Change Products Function***************************************/

function productChategorySelect() {
	document.getElementById("default_opt").style.display = "none";
	document.getElementById("cate_opt").style.display = "block";
	var url ="productCreateCatListAjax.php";
	var _XH = createXmlHttp();
	_XH.onreadystatechange=function() {
		if(_XH.readyState==4 || _XH.readyState=="complete") {
			document.getElementById('cate_opt').innerHTML = _XH.responseText;
		} else {
			document.getElementById('cate_opt').innerHTML = "Please wait ...";
		}
	}
	_XH.open("GET",url,true);
	_XH.send(null);
}

function productSelectCancel() {
	document.getElementById("default_opt").style.display = "block";
	document.getElementById("cate_opt").style.display = "none";
}

function productChategoryChanged(id) {
	document.getElementById("default_opt").style.display = "none";
	document.getElementById("cate_opt").style.display = "block";
	var id = parseInt(id);
	var url ="productCreateListAjax.php";
	if(id > 0) {
		url = url+"?id="+id;
	}
	var _XH = createXmlHttp();
	_XH.onreadystatechange=function() {
		if(_XH.readyState==4 || _XH.readyState=="complete") {
			document.getElementById('cate_opt').innerHTML = _XH.responseText;
		} else {
			document.getElementById('cate_opt').innerHTML = "Please wait ...";
		}
	}
	_XH.open("GET",url,true);
	_XH.send(null);
}

function productShowDescAjax(id) {
	var id = id;
	var url ="productCreateDetailsAjax.php";
	url = url+"?id="+id;
	var _XH = createXmlHttp();
	_XH.onreadystatechange=function() {
		if(_XH.readyState==4 || _XH.readyState=="complete") {
			document.getElementById('cate_opt').innerHTML = _XH.responseText;
		} else {
			document.getElementById('cate_opt').innerHTML = "Please wait ...";
		}
	}
	_XH.open("GET",url,true);
	_XH.send(null);
}

function productChategorySelectAjax() {
	var url ="productCreateCatListAjax.php";
//	url = url+"?id="+id;
	var _XH = createXmlHttp();
	_XH.onreadystatechange=function() {
		if(_XH.readyState==4 || _XH.readyState=="complete") {
			document.getElementById('cate_opt').innerHTML = _XH.responseText;
		} else {
			document.getElementById('cate_opt').innerHTML = "Please wait ...";
		}
	}
	_XH.open("GET",url,true);
	_XH.send(null);
}


/********************************Change Products Function***************************************/

function HidePlzWaitDiv(){
	document.getElementById('plswaitDIV').style.display='none';
}
function ShowPlzWaitDiv(){
	document.getElementById('plswaitDIV').style.display='block';
}
/********************************AJAX Function***************************************/

function createXmlHttp() {
    var xmlHttp;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
    return xmlHttp;
}

// call this
function doAjaxFetch_Weight() {
	var countryZoneID = 'h_CountryZone_'+document.getElementById('CountryList').value;
	var countryZone = document.getElementById(countryZoneID).value;
	document.getElementById('h_Zone').value = countryZone;
	
	var weight = parseInt(document.getElementById('qty_total').innerHTML)*document.getElementById('h_Product_Weight').value;
   
	var url = 'weight_controller.php?kg='+weight+'&zone='+document.getElementById('h_Zone').value;
	
	var _XH = createXmlHttp();
	_XH.onreadystatechange=function() {
		if(_XH.readyState==4 || _XH.readyState=="complete") {
			
			document.getElementById('h_Product_Weight_Rate').value = _XH.responseText;
			HidePlzWaitDiv();
			document.getElementById('h_AjaxRequestStatus').value =1;
			countryChanged();	
		}
	}
	_XH.open("GET",url,true);
	_XH.send(null);
}

// call this
function pre_doAjaxFetch_Weight(p_Doc) {
	//alert(document.getElementById('h_Product_Weight').value);
	if(p_Doc.getElementById('CountryList').value !=11){
		var weight = parseInt(p_Doc.getElementById('qty_total').innerHTML)*p_Doc.getElementById('h_Product_Weight').value;
		//doAjaxFetch_Weight(weight);
		//alert(weight);
		var url = 'weight_controller.php?kg='+weight+'&zone='+p_Doc.getElementById('h_Zone').value;
		
		var _XH = createXmlHttp();
		_XH.onreadystatechange=function() {
			if(_XH.readyState==4 || _XH.readyState=="complete") {
				p_Doc.getElementById('h_Product_Weight_Rate').value = _XH.responseText;
				//HidePlzWaitDiv();
				pre_countryChanged_Ajax(p_Doc);
			}
		}
		_XH.open("GET",url,true);
		_XH.send(null);
	}
	else{
		p_Doc.getElementById('h_Product_Weight_Rate').value = 0;
		pre_countryChanged_Ajax(p_Doc);
	}
}

/*
* From xselect.js
*/
function setupXSelect(p_DivID) {
	p_DivID += '_XSELECT';
	//alert(p_DivID);
	var xSelect = document.getElementById(p_DivID);
	if(!xSelect) return;
	/* set items box position */

	var itemsDiv = document.getElementById(xSelect.getAttribute('items'));
	/* set event handlers for showing/hiding the items */
	xSelect.onclick = new Function("showItems('"+p_DivID+"', '"+xSelect.getAttribute('items')+"', true)");
	xSelect.onmouseover = new Function("mouseEntered('"+p_DivID+"', false)");
	xSelect.onmouseout = new Function("mouseExited('"+p_DivID+"', false)");
	itemsDiv.onmouseover = new Function("mouseEntered('"+p_DivID+"', false)");
	itemsDiv.onmouseout = new Function("mouseExited('"+p_DivID+"', false)");
	
	/* set event handlers for item selection */
	var formElem = document.getElementById(xSelect.getAttribute('formelement'));
	var items = itemsDiv.getElementsByTagName('div');
	var i = 0;
	for(i=0; i<items.length; i++) {
		items[i].onclick = new Function("itemSelected('"+p_DivID+"', this.getAttribute('value'), this.innerHTML)");
		items[i].onmouseover = new Function("mouseEntered('"+p_DivID+"', true)");
		// set default selected value
		if(i==0 || items[i].getAttribute('selected')=='true') {
			xSelect.setAttribute('value', items[i].getAttribute('value'));
			xSelect.setAttribute('style', items[i].getAttribute('style'));
			xSelect.innerHTML = items[i].innerHTML;
			formElem.value = items[i].getAttribute('value');
		}
	}
}

function showItems(p_DivID, p_ItemsDivID, p_Show) {
	var xSelect = document.getElementById(p_DivID);
	if(!xSelect) return;
//	xSelect.focus();
	
	var itemsDiv = document.getElementById(p_ItemsDivID);
	if(!itemsDiv) return;
	if(p_Show) {
		itemsDiv.style.display = 'block';
		xSelect.parentNode.style.zIndex = 2;
	}
	else {
		if(xSelect.getAttribute('hideitems')=='1') {
			itemsDiv.style.display = 'none';
			xSelect.parentNode.style.zIndex = 1;
		}
	}
}

function mouseEntered(p_DivID, p_Item) {
	var xSelect = document.getElementById(p_DivID);
	if(!xSelect) return;
	xSelect.setAttribute('hideitems', '0');
}

function mouseExited(p_DivID, p_Item) {
	var xSelect = document.getElementById(p_DivID);
	if(!xSelect) return;
	xSelect.setAttribute('hideitems', '1');
	var hideJS = "showItems('"+p_DivID+"', '"+xSelect.getAttribute('items')+"', false);";
	setTimeout(hideJS, 1000);
}

function itemSelected(p_DivID, p_NewValue, p_NewContent) {
	var xSelect = document.getElementById(p_DivID);
	if(!xSelect) return;
	
	if(p_NewValue != null || p_NewValue != ''){
	
		xSelect.setAttribute('value', p_NewValue);
		
		xSelect.innerHTML = p_NewContent;
	}
	var formElem = document.getElementById(xSelect.getAttribute('formelement'));
	formElem.value = p_NewValue;
	
	xSelect.setAttribute('hideitems', '1');
	showItems(p_DivID, xSelect.getAttribute('items'), false);
	
	if(xSelect.getAttribute('onchange') && xSelect.getAttribute('onchange')!='') {
		eval(xSelect.getAttribute('onchange'));
	}
}

function setupCountrySelect(p_DivID) {
	p_DivID += '_XSELECT';
	var xSelect = document.getElementById(p_DivID);
	if(!xSelect) return;

	/* set items box position */
	var itemsDiv = document.getElementById(xSelect.getAttribute('items'));
	
	/* set event handlers for showing/hiding the items */
	xSelect.onclick = new Function("showItems('"+p_DivID+"', '"+xSelect.getAttribute('items')+"', true)");
	xSelect.onmouseover = new Function("mouseEntered('"+p_DivID+"', false)");
	xSelect.onmouseout = new Function("mouseExited('"+p_DivID+"', false)");
	itemsDiv.onmouseover = new Function("mouseEntered('"+p_DivID+"', false)");
	itemsDiv.onmouseout = new Function("mouseExited('"+p_DivID+"', false)");
	
	/* set event handlers for item selection */
	var formElem = document.getElementById(xSelect.getAttribute('formelement'));
	var items = itemsDiv.getElementsByTagName('div');
	var i = 0;
	for(i=0; i<items.length; i++) {
		items[i].onclick = new Function("countrySelected('"+p_DivID+"', this.getAttribute('value'), this.innerHTML)");
		items[i].onmouseover = new Function("mouseEntered('"+p_DivID+"', true)");
		
		// set default selected value
		if(i==0 || items[i].getAttribute('selected')=='true') {
			xSelect.setAttribute('value', items[i].getAttribute('value'));
			xSelect.innerHTML = items[i].innerHTML;
			formElem.value = items[i].getAttribute('value');
		}
	}
}

function countrySelected(p_DivID, p_NewValue, p_NewContent) {
	var xSelect = document.getElementById(p_DivID);
	if(!xSelect) return;
	xSelect.setAttribute('value', p_NewValue);
	
	xSelect.innerHTML = p_NewContent;
	
	var formElem = document.getElementById(xSelect.getAttribute('formelement'));
	formElem.value = p_NewValue;
	
	xSelect.setAttribute('hideitems', '1');
	showItems(p_DivID, xSelect.getAttribute('items'), false);
	
	if(xSelect.getAttribute('onchange') && xSelect.getAttribute('onchange')!='') {
		eval(xSelect.getAttribute('onchange'));
	}
	onCountryChange(p_NewValue, p_NewContent);
}

/*
* End
*/

/*
* From helpers.js: Start here
*/
function radioButtonClicked(p_ID, p_Index, p_Count) {
	for(var i=0; i<p_Count; i++) {
		var id=p_ID+'_'+i+'_link';
		document.getElementById(id).className = 'HLink_Radio0';
	}
	document.getElementById(p_ID+'_'+p_Index+'_link').className = 'HLink_Radio1';
	document.getElementById(p_ID).value = p_Index;
}


function hPrintRadioButtonGroup(p_ID, p_Index, p_Count) {
	for(var i=0; i<p_Count; i++) {
		var id=p_ID+'_'+i+'_link';
		document.getElementById(id).className = 'HPLink_Radio0';
	}
	document.getElementById(p_ID+'_'+p_Index+'_link').className = 'HPLink_Radio1';
	document.getElementById(p_ID).value = p_Index;
}
function hIllusRadioButtonGroup(p_ID, p_Index, p_Count) {
	/*
	for(var i=0; i<p_Count; i++) {
	var id=p_ID+'_'+i;
	document.getElementById(id).src = 'images/rad0.gif';
	}
	document.getElementById(p_ID+'_'+p_Index).src = 'images/rad1.gif';
	*/
	for(var i=0; i<p_Count; i++) {
		var id=p_ID+'_'+i+'_link';
		document.getElementById(id).className = 'HPLink_Radio2';
	}
	document.getElementById(p_ID+'_'+p_Index+'_link').className = 'HPLink_Radio1';
	document.getElementById(p_ID).value = p_Index;
}
function radioButtonDeselectAll(p_ID, p_Count) {
	/*
	for(var i=0; i<p_Count; i++) {
	var id=p_ID+'_'+i;
	document.getElementById(id).src = 'images/rad0.gif';
	}
	*/
	for(var i=0; i<p_Count; i++) {
	var id=p_ID+'_'+i+'_link';
		document.getElementById(id).className = 'HLink_Radio0';
	}
	document.getElementById(p_ID).value = -1;
}
function appendRow(p_IDPrefix,fontchoser,fontsize,colorchoser,fontstylechoser) {
	var rows = document.getElementById(p_IDPrefix).getElementsByTagName('tr').length;
	rows=rows+1;
	
	var ctrlCount=parseInt(document.getElementById('h_DS_textCount').value);
	document.getElementById('h_DS_textCount').value=(ctrlCount+1);
	
	fontchoser=fontchoser.replace(p_IDPrefix+'Fontchoose',p_IDPrefix+'_text_font_'+rows);
	fontchoser=fontchoser.replace(p_IDPrefix+'Fontchoose',p_IDPrefix+'_text_font_'+rows);
	fontsize=fontsize.replace(p_IDPrefix+'FontSize',p_IDPrefix+'_text_fontsize_'+rows);
	fontsize=fontsize.replace(p_IDPrefix+'FontSize',p_IDPrefix+'_text_fontsize_'+rows);
	colorchoser=colorchoser.replace(p_IDPrefix+'ColorChoose',p_IDPrefix+'_text_color_'+rows);
	colorchoser=colorchoser.replace(p_IDPrefix+'ColorChoose',p_IDPrefix+'_text_color_'+rows);
	fontstylechoser=fontstylechoser.replace(p_IDPrefix+'FontStyle',p_IDPrefix+'_text_style_'+rows);
	fontstylechoser=fontstylechoser.replace(p_IDPrefix+'FontStyle',p_IDPrefix+'_text_style_'+rows);
	
	var tbody = document.getElementById(p_IDPrefix).getElementsByTagName("tbody")[0];
	var row1 = document.createElement("TR");
	var cell1 = document.createElement("TD");
	cell1.innerHTML = "<td colspan='4'><input class='DS_TEXT' width='100%' type='text' name='"+p_IDPrefix+"_ds_text_"+rows+"' id='"+p_IDPrefix+"_ds_text_"+rows+"'></td>";
	var row = document.createElement("TR");
	var cell2 = document.createElement("TD");
	cell2.innerHTML = "<td style='border: 0px;'>"+fontchoser+"</td>";
	var cell3 = document.createElement("TD");
	cell3.innerHTML = "<td style='border: 0px;'>"+fontsize+"</td>";
	var cell4 = document.createElement("TD");
	cell4.innerHTML = "<td style='border: 0px;'>"+colorchoser+"</td>";
	var cell5 = document.createElement("TD");
	cell5.innerHTML = "<td style='border: 0px;'>"+fontstylechoser+"</td>";
	var cell6 = document.createElement("TD");
	cell6.innerHTML = "<img src='images/remove.jpg' onclick='delRow("+rows+","+p_IDPrefix+")' alt='Remove'>";
	row1.appendChild(cell1);
	row.appendChild(cell2);
	row.appendChild(cell3);
	row.appendChild(cell4);
	row.appendChild(cell5);
	row.appendChild(cell6);
	tbody.appendChild(row1);
	tbody.appendChild(row);
}
function delRow(val,tblID) {
	var ctrlCount=parseInt(document.getElementById('h_DS_textCount').value);
	document.getElementById('h_DS_textCount').value=(ctrlCount-1);
	tblID.deleteRow((val-1));
	tblID.deleteRow((val-1));
}
function appendIllustratorRow(p_IDPrefix) {
	var rows = document.getElementById('ill'+p_IDPrefix).getElementsByTagName('tr').length;
	rows=rows+1;
	var tbody = document.getElementById('ill'+p_IDPrefix).getElementsByTagName("tbody")[0];
	var row = document.createElement("TR");
	var cell1 = document.createElement("TD");
	cell1.innerHTML = "<td colspan='4' class='STEPCONTTD_LINE'>"+RadioButtonGroup+"</td>";
	var cell2 = document.createElement("TD");
	var celhtml="<td align='center'><div width='100%' style='display: none;' id='"+$p_IDPrefix+"_ilus_ilus_1' name='"+$p_IDPrefix+"_ilus_ilus_1'>"+IllusCategoryChoser+""+IllusImageChoser+"</div>";
	celhtml+="<div width='100%' style='display: none;' id='"+p_IDPrefix+"_ilus_imag_1' name='"+p_IDPrefix+"_ilus_imag_1'><input class='ILLUSIMAGECHOOSERINPUT' type='file' id='"+p_IDPrefix+"_ilus_imag_1 name='"+p_IDPrefix+"_ilus_imag_1 /></div></td>";
	cell2.innerHTML = celhtml;
	row.appendChild(cell1);
	row.appendChild(cell2);
	tbody.appendChild(row);
}
/*
* From helpers.js: end here
*/


	function showOverLayFrame(p_Left, p_Top, p_Width, p_Height) {
		var olay = document.getElementById('overlayFrame');
		if(olay) {
			if(p_Top!=-1) {
				olay.style.left=p_Left+'px';
				olay.style.top=p_Top+'px';
				olay.style.width=p_Width+'px';
				olay.style.height=p_Height+'px';
			}
			olay.style.display='block';
		}
	}
	function hideOverLayFrame() {
		var olay = document.getElementById('overlayFrame');
		if(olay) {
			olay.style.display='none';
		}
	}
	function showPopupMenu(p_MenuID, p_Parent) {
		if(dragEnabled || sizeEnabled) return;
		var parL;
		var parT;
		var obj = p_Parent;
		if (obj.offsetParent) {
			parL = obj.offsetLeft;
			parT = obj.offsetTop;
			while (obj = obj.offsetParent) {
				parL += obj.offsetLeft;
				parT += obj.offsetTop;
			}
		}
		parL = parL - 13;
		parT = parT + p_Parent.offsetHeight-2;
		
		var menuObj = document.getElementById(p_MenuID);
		menuObj.style.left = parL + 'px';
		menuObj.style.top = parT + 'px';
		menuObj.style.display = 'block';
		
		showOverLayFrame(parL, parT+7, menuObj.offsetWidth, menuObj.offsetHeight-14);
	}
	function hidePopupMenu(p_MenuID) {
		hideOverLayFrame();
		var menuObj = document.getElementById(p_MenuID);
		menuObj.style.display = 'none';
	}
	function showMe(p_MenuObj) {
		showOverLayFrame(-1, -1, -1, -1);
		p_MenuObj.style.display = 'block'
	}
	function hideMe(p_MenuObj) {
		hideOverLayFrame();
		p_MenuObj.style.display = 'none'
	}
	window.history.forward(1);

