function allowInteger(obj){
	if(obj.value != ""){
		var re2digit  = /(^-?\d\d*$)/;	
		if (obj.value.search(re2digit)==-1){ //if match failed
		alert("Please enter a whole number.")
				return false;
		}
		
		return true;
	}
		
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function mat_options_PopUp(fabric, width, height)
{
    window.open("mat_options.php?type="+fabric+"", "Mat_Options", "toolbar=no, menubar=no, width="+width+", height="+height+", status=no, scrollbars=yes", false)
}


function openswitcher(id){

	if(document.getElementById("id_"+id).style.display == 'block'){
	document.getElementById("id_"+id).style.display = 'none'
	}else{	
	document.getElementById("id_fabric").style.display = 'none'
	document.getElementById("id_uv").style.display = 'none'
	document.getElementById("id_"+id).style.display = 'block'
	}
}

function matselected(id,srce,title,price,optval,cl,pPrice,currency, cur_sign,imgsrcs,curr_charges){

		if(document.Form.MID){
		var mat_pid = document.Form.MID.value;
		if(mat_pid=='1005' || mat_pid=='1006')
		{
			customPrice = new Array(3)
			customPrice["10"]=109.95
			customPrice["12"]=119.95
			customPrice["14"]=144.95
			var mesunit = 0;

			if (eval(document.getElementById('Length1').value) > eval(document.getElementById('Width1').value))
			{
				mesunit = document.getElementById('Length1').value;
			}
			else
			{
				mesunit = document.getElementById('Width1').value;
			}

			if(mat_pid=='1006')
			{
				if ((mesunit>0) && (mesunit<=150))
				{
					pPrice= customPrice["10"];
				}
				if ((mesunit>150) && (mesunit<=180))
				{
					pPrice = customPrice["12"];
				}
				if ((mesunit>180) && (mesunit<=230))
				{
					pPrice= customPrice["14"];
				}
			}
			else if(mat_pid=='1005')
				{
				if ((mesunit>0) && (mesunit<=130))
				{
					pPrice= customPrice["10"];
				}
				if ((mesunit>130) && (mesunit<=160))
				{
					pPrice = customPrice["12"];
				}
				if ((mesunit>160) && (mesunit<=200))
				{
					pPrice= customPrice["14"];
				}
			}
		}
		}
		var prc = '';

		
		if(title=='Jet Black Fabric' || title=='None'){
			prc='<br> + '+cur_sign+' '+0;
		}

		if(curr_charges != ''){
			curr_charges = curr_charges/100;
		}
		//alert('minhaj'+curr_charges);
		if(price != 0)
			 prc='<br> + '+cur_sign+' '+round_decimals((price*currency + price*currency*curr_charges ), 2);


		if(optval == 'UVProtection' && title !='None'){
			var srce1 = (document.getElementById("Img_"+imgsrcs).src).split('.jpg');
			var srce =srce1[0];
		}


		if(cl==2){
		document.getElementById("imgsrc_"+id).src=srce+".jpg";
		document.getElementById("text_"+id).innerHTML = title+ prc;
		
		if(optval == 'UVProtection' && title =='None'){
		document.getElementById("waranty_text").innerHTML = "<span>&nbsp;</span>";
		}else if(optval == 'UVProtection' && title =='DuraClip'){
		document.getElementById("waranty_text").innerHTML = "Adds 3-yrs to perimeter warranty.";
		}else if(optval == 'UVProtection'){
		document.getElementById("waranty_text").innerHTML = "Adds 2-yrs to perimeter warranty.";
		}
		
		}else if(cl==1){
		document.getElementById("imgsrc_"+id).src=srce+".jpg";
		document.getElementById("mainid_"+id).src=srce+".jpg";
		
		if(optval == 'UVProtection' && title !='None'){
		document.getElementById("mainidtext_"+id).innerHTML= title+ prc;
		}else{
		document.getElementById("mainidtext_"+id).innerHTML= title+ prc;
		}

		if(optval == 'UVProtection' && title =='None'){
		document.getElementById("warranty_uv").innerHTML = "<span>&nbsp;</span>";
		document.getElementById("waranty_text").innerHTML = "<span>&nbsp;</span>";
		}else if(optval == 'UVProtection' && title =='DuraClip'){
		document.getElementById("warranty_uv").innerHTML = "Adds 3-yrs to perimeter warranty.";
		document.getElementById("waranty_text").innerHTML = "Adds 3-yrs to perimeter warranty.";
		}else if(optval == 'UVProtection'){
		document.getElementById("warranty_uv").innerHTML = "Adds 2-yrs to perimeter warranty.";
		document.getElementById("waranty_text").innerHTML = "Adds 2-yrs to perimeter warranty.";
		}
		
		document.getElementById("text_"+id).innerHTML = title+ prc;
		document.getElementById('nn_'+optval).value = title;
		
		document.getElementById("price_"+optval).value = round_decimals((price*currency + price*currency*curr_charges ), 2);
		add_price_colorswatch(optval, pPrice, currency, cur_sign);
		
		if(optval == 'Fabric'){

			if(document.getElementById('Img_pecan_tan_sunshield')){
					document.getElementById('Img_pecan_tan_sunshield').src= srce + '_pecan_tan_sunshield.jpg';
			}
			if(document.getElementById('Img_olive_green_sunshield')){
					document.getElementById('Img_olive_green_sunshield').src= srce + '_olive_green.jpg';
			}
			if(document.getElementById('Img_pacific_blue_sunshield')){
					document.getElementById('Img_pacific_blue_sunshield').src= srce + '_pacific_blue_sunshield.jpg';
			}
			if(document.getElementById('Img_snowball_white_sunshield')){
					document.getElementById('Img_snowball_white_sunshield').src= srce + '_snowball_white_sunshield.jpg';
			}
			if(document.getElementById('Img_jet_black_sunshield')){
					document.getElementById('Img_jet_black_sunshield').src= srce + '_jet_black_sunshield.jpg';
			}
			if(document.getElementById('remove_uv_image')){
					//document.getElementById('remove_uv_image').value= srce + '_none.jpg';
					document.getElementById('remove_uv_image').value= document.getElementById('remove_uv_image').value;
			}
			if(document.getElementById('Img_duraclip')){
			document.getElementById('Img_duraclip').src= srce + '_duraclip.jpg';
			}
		}
		openswitcher(id);
	}

	if(title == 'None'){
		var nonesel =document.getElementById('remove_uv_image').value;
		document.getElementById("imgsrc_"+id).src= nonesel;
		document.getElementById("mainid_"+id).src= nonesel;		
		document.getElementById("id_"+id).style.display= 'none';		
	}
}

function matselectedout(id,srce,title,price,optval,cl,pPrice,currency, cur_sign,imgsrcs){
document.getElementById("imgsrc_"+id).src=document.getElementById("mainid_"+id).src;
var ttsst=document.getElementById("mainidtext_"+id).innerHTML;

	if(ttsst != '<font color="#ff0000">You have not yet selected any fabric. Please select one.</font>'){
		document.getElementById("text_"+id).innerHTML=ttsst;

		if(id == 'uv')
		document.getElementById("waranty_text").innerHTML = document.getElementById("warranty_"+id).innerHTML;
	}
}

function add_price_colorswatch(optval, pPrice,currency, cur_sign)
{
	var totPrice = pPrice;
	
	if(document.getElementById('nn_Fabric').value != ''){
		totPrice =(parseFloat(totPrice)+parseFloat(document.getElementById("price_Fabric").value));
		document.Form.txtPrice.value=totPrice;
		document.Form.txtPricebottom.value=totPrice;
	}

	if(document.getElementById('nn_UVProtection').value != ''){
		totPrice =(parseFloat(totPrice)+parseFloat(document.getElementById("price_UVProtection").value));
		document.Form.txtPrice.value=totPrice;
		document.Form.txtPricebottom.value=totPrice;
	}
		
	var MID = document.Form.MID.value;
	var form = document.Form;
	
	if(MID=='1003'){
		qObj = document.Form.pquantity;
		var pqty = qObj.value;
	}
	else{
	qObj = document.all("quantity["+MID+"]");
	var pqty = qObj.value;	
	}
	
	if(pqty == '')
	{
		pqty = 1;
	}

	document.Form.txtPrice.value = cur_sign+""+round_decimals((document.Form.txtPrice.value * pqty), 2);
	document.Form.txtPricebottom.value = cur_sign+""+round_decimals((document.Form.txtPricebottom.value * pqty), 2);
}

function chk_fabric(type){	
	
	if(document.getElementById("mainidtext_"+type).innerHTML=='' && type=='fabric'){
		document.getElementById("mainidtext_"+type).innerHTML="<font color='#ff0000'>You have not yet selected any fabric. Please select one.</font>";
	}
}
function round_decimals(original_number, decimals) 
{
	var result1 = original_number * Math.pow(10, decimals)
	var result2 = Math.round(result1)
	var result3 = result2 / Math.pow(10, decimals)
	return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) 
{
	var value_string = rounded_value.toString()
    var decimal_location = value_string.indexOf(".")
    if (decimal_location == -1) 
	{
		decimal_part_length = 0
        value_string += decimal_places > 0 ? "." : ""
    }
    else 
	{
        decimal_part_length = value_string.length - decimal_location - 1
    }
    var pad_total = decimal_places - decimal_part_length
    if (pad_total > 0) 
	{
        for (var counter = 1; counter <= pad_total; counter++)
            value_string += "0"
	}
    return value_string
}