var loader_img = "<img style='margin:7px 0 0 40px' src=\"images/ajax-loader.gif\">";
var sizeloader_img = "<img src=\"images/ajax-loader.gif\" align='absmiddle'> <span style='color:#AC1313;'>Updating available colours</span>";
function select_matching_color(size) {
	$("quantity").disabled = true;
	//$("loader_id").innerHTML = sizeloader_img;
	$("colorback").innerHTML = loader_img;
	var product_id = $("product_id").value;
	var attributeid = document.getElementById('product_details_colour').value.split("#");
	var success = function(t){select_matching_success(t,size,product_id,attributeid[0],attributeid[1]);}
	var failure = function(t){select_matching_failed(t,size,product_id);}
	var pars    = 'action=size_based&size='+size+'&product_id='+product_id+'&attribute_id='+attributeid[0];
	var url		= GLOBALPATH+"clothing/ajax/fashion_products.php";
	var myAjax  = new Ajax.Request(url, {method:'post',postBody:pars, onSuccess:success, onFailure:failure});
}

function select_matching_success(t,size,product_id,attributeid,colorname) {
	$("colorback").innerHTML = '<input type="text" name="product_details_colr" value="Colour" id="product_details_colr" class="colr_txtbox" readonly />';
	$("product_details_colr").value = 'Color';
	var strvalue = t.responseText.split("@@");
	//getcolor_details(size,product_id);
	getcolor_images(size,product_id,colorname);
	$('size').value = size;
	if(attributeid != "sel") {
		if(strvalue[0] != "") {
			$('p_price').innerHTML = '£'+strvalue[0];
		}
		if(strvalue[2] != "") {
			$('p_code').innerHTML = strvalue[2];
		}
	}
	$("disabledropdown").style.display = 'block';
	$("loader_id").innerHTML = '';
	setTimeout("var attribute_id = document.getElementById('product_details_colour').value.split('#');var productid = $('product_id').value;var size = $('size').value;if(attribute_id != 'sel'){updatecolorimages(size,productid,attribute_id[1]);}", 1000);
}
function update_colordetails(size,productid,attribute_id) {
	$("loader_id").innerHTML = sizeloader_img;
	var success = function(t){update_colordetails_success(t,size,productid,attribute_id);}
	var failure = function(t){update_colordetails_failed(t,size,product_id);}
	var pars    = 'action=size_based&size='+size+'&product_id='+productid+'&attribute_id='+attribute_id;
	var url		= GLOBALPATH+"clothing/ajax/fashion_products.php";
	var myAjax  = new Ajax.Request(url, {method:'post',postBody:pars, onSuccess:success, onFailure:failure});
}

function update_colordetails_success(t,size,product_id,attributeid) {
	var strvalue = t.responseText.split("@@");
	var color = document.getElementById('product_details_colour').value;
	if(attributeid != "sel") {
		if(strvalue[0] != '') {
			$('p_price').innerHTML = '£'+strvalue[0];
		}
		if(strvalue[2] != '') {
			$('p_code').innerHTML = strvalue[2];
		}
	} 
	$("loader_id").innerHTML = '';
}
function getcolor_details(size,product_id) {
	var selcolor = document.getElementById("product_details_colour").value.split("#");
	var success = function(t){ getcolor_details_success(t,selcolor[1]);}
	var failure = function(t){ getcolor_details_failed(t,size,product_id);}
	
	var pars    = 'action=color_details&size='+size+'&product_id='+product_id;
	var url		= GLOBALPATH+"clothing/ajax/fashion_products.php";
	var myAjax  = new Ajax.Request(url, {method:'post',postBody:pars, onSuccess:success, onFailure:failure});
}

function getcolor_details_success(t,selcolor) {
	//document.getElementById("product_details_colour").value     = "";
    document.getElementById("product_details_colour").innerHTML = "";
	objList     = "";
	objList     = document.getElementById("product_details_colour");
	objXML 		= t.responseXML;
	objElements = objXML.getElementsByTagName("product_details_colour");
	iNode       = objElements.length;
	for(var i=0;i<objElements.length;i++) {
		thisNode      = objElements.item(i);
		var model1	  = thisNode.getAttribute("AttributeDetailID").toString();
		var model     = thisNode.getAttribute("ColourDescription").toString();
		var selected  = thisNode.getAttribute("Selected");
		var newOption = document.createElement("OPTION");
		objList.appendChild(newOption);
		newOption.value = model1;
		newOption.text  = model;
		var selectedid = model1.split("#");
		if(selectedid[1] == selcolor)
			newOption.selected = selcolor;
	}
}

function getcolor_images(size,product_id) {
	var success = function(t){ getcolor_images_success(t,size,product_id);}
	var failure = function(t){ getcolor_images_failed(t,size,product_id);}
	
	var pars    = 'action=color_images&size='+size+'&product_id='+product_id;
	var url		= GLOBALPATH+"clothing/ajax/fashion_products.php";
	var myAjax  = new Ajax.Request(url, {method:'post',postBody:pars, onSuccess:success, onFailure:failure});
}

function getcolor_images_success(t) {
	var strvalue = t.responseText.split("##");
	$("listcolr").innerHTML = strvalue[0];
	if(document.getElementById('listcolr'))
		document.getElementById('listcolr').style.width = (document.getElementById('clor_tot').value * 107)+'px';
	//$("p_colorimage").innerHTML = "<img src="+GLOBALPATH+"products/colorpicker/medium/"+strvalue[1]+">";
	//$("colorname").innerHTML = strvalue[2]; 

}
function updatecolorimages(size,product_id,colorcode) {
	var success = function(t){ updatecolorimages_success(t,colorcode);}
	var failure = function(t){ updatecolorimagesfailed(t);}
	
	var pars    = 'action=getcolor_images&size='+size+'&product_id='+product_id+'&colorcode='+colorcode;
	var url		= GLOBALPATH+"clothing/ajax/fashion_products.php";
	var myAjax  = new Ajax.Request(url, {method:'post',postBody:pars, onSuccess:success, onFailure:failure});
}

function updatecolorimages_success(t,colorcode) {
	var attribute_id = document.getElementById('product_details_colour').value.split('#');
	if(attribute_id != "sel") {
		var strvalue = t.responseText.split("##");
		$("p_colorimage").innerHTML = "<img src="+GLOBALPATH+"products/colorpicker/medium/"+strvalue[1]+">";
		$("colorname").innerHTML = strvalue[2]; 
	}
}
function select_mathch_color(colrval, colrcde,sel, tot){
	if(colrval.length > 8){
		var tempval = colrval.length - 8;
		document.getElementById('colorback').style.width = 100+(tempval*9)+'px';
	}
	document.getElementById('product_details_colr').value= colrval;
	select_matching_size(colrcde);
	for(var i=1;i<=tot;i++){
		document.getElementById("clr_"+i).className = '';
	}
	document.getElementById("clr_"+sel).className = 'selectd';
}

function select_matching_size(ident) {
	var size = 'unchecked';
	/* for(var i=0; i < document.product_details.product_fragrance_size.length; i++) {
		if(document.product_details.product_fragrance_size[i].checked) {
			size = document.product_details.product_fragrance_size[i].value;
		}
	}*/
	size = document.getElementById('product_fragrance_size').options[document.getElementById('product_fragrance_size').selectedIndex].value;
	var attribute_id = ident.split("#");
	//if(attribute_id[0]=="sel")
		//return false;

	//$("match_loader_id").innerHTML = loader_img;
	var product_id = $("product_id").value;
	var success = function(t){ select_matching_size_success(t,ident,size);}
	var failure = function(t){ select_matching_size_failed(t);}
	
	var pars    = 'action=colour_based&ident='+attribute_id[0]+'&product_id='+product_id;
	var url		= GLOBALPATH+"clothing/ajax/fashion_products.php";
	var myAjax  = new Ajax.Request(url, {method:'post',postBody:pars, onSuccess:success, onFailure:failure});
}	

function select_matching_size_success(t,ident,size) {
	$("quantity").disabled = false;
	var strvalue = t.responseText.split("@@");
	if(ident != "sel") {
		$('p_price').innerHTML = '&pound;'+strvalue[0];
		$('p_code').innerHTML = strvalue[2];
		$("product_details_colour").value = ident;		// selected the selec box.		

		if(strvalue[3]!='') {
		   $("p_colorimage").innerHTML = "<img src="+GLOBALPATH+"products/colorpicker/medium/"+strvalue[3]+">" 
		   $('colorname').innerHTML = strvalue[4];
		} else {
			var firstcolor  	 =  $("first_color").value;
			var firstcolor_desc  =  $("first_color_desc").value;
			$("p_colorimage").innerHTML = "<img src="+GLOBALPATH+"products/colorpicker/medium/"+firstcolor+">" 
			$("colorname").innerHTML = firstcolor_desc;
		}
	}
	$("match_loader_id").innerHTML = '';
	
}

function Validate_cart() {
	var chkflag = 0;
	if(document.getElementById('product_fragrance_size').options[document.getElementById('product_fragrance_size').selectedIndex].value != '') {
		chkflag = 1;
	}
	if(chkflag == '0') {
		alert("Please select a size");
		return false;	
	}
	if($("product_details_colour").value=='' || $("product_details_colour").value=="sel" || $("product_details_colr").value == 'Color') {
		alert("Please select Colour");
		//$("product_details_colour").focus();
		return false;
	}
	if($("quantity").value=='' || $("quantity").value=="sel" ) {
		alert("Please select Quantity");
		$("quantity").focus();
		return false;
	}
	return true;
}

function viewimage(Imagename) {
	 document.getElementById('submt').value = '';	
	 document.product_details.image_change.value = Imagename;
	 document.product_details.submit();
}

function pickerinfo(imagename,colorname,type,attributeid) {
	$("p_colorimage").innerHTML = "<img src="+GLOBALPATH+"products/colorpicker/medium/"+imagename+">" ;
	$("colorname").innerHTML = colorname;
	// $("product_details_colour").value = attributeid;
}

function pickerinfohide() {
	var firstcolor  	 =  $("first_color").value;
	var firstcolor_desc  =  $("first_color_desc").value;
	$("p_colorimage").innerHTML = "<img src="+GLOBALPATH+"products/colorpicker/medium/"+firstcolor+">" 
	$("colorname").innerHTML = firstcolor_desc;
}

function tab_contain(position) {
	if(position=='left') {
		$("tabinfo-left").style.display="block";
		$("left_container").className = "product_tabinfo_lt_active";
		$("tabinfo-right").style.display="none";
		$("right_container").className = "product_tabinfo_rt";
	} else {
		$("tabinfo-left").style.display="none";
		$("left_container").className = "product_tabinfo_lt";
		$("tabinfo-right").style.display="block";
		$("right_container").className = "product_tabinfo_rt_active";
	}
}

function new_tab_contain(position) {
	if(position=='left') {
		$("new_tabinfo_left").style.display="block";
		$("new_left_container").className = "new_product_tabinfo_lt_active";
		$("new_tabinfo_right").style.display="none";
		$("new_tabinfo_middle").style.display="none";
		$("new_mid_container").className = "new_product_tabinfo_mid";
		$("new_right_container").className = "new_product_tabinfo_rt";
		$("sep_1").className = "seperator_selected";
		$("sep_2").className = "seperator_normal";
	} else if(position=='mid') {
		$("new_tabinfo_middle").style.display="block";
		$("new_mid_container").className = "new_product_tabinfo_mid_active";
		$("new_tabinfo_right").style.display="none";
		$("new_tabinfo_left").style.display="none";
		$("new_left_container").className = "new_product_tabinfo_lt";
		$("new_right_container").className = "new_product_tabinfo_rt";
		$("sep_1").className = "seperator_normal";
		$("sep_2").className = "seperator_selected";
	} else {
		$("new_tabinfo_middle").style.display="none";
		$("new_tabinfo_left").style.display="none";
		$("new_left_container").className = "new_product_tabinfo_lt";
		$("new_mid_container").className = "new_product_tabinfo_mid";
		$("new_tabinfo_right").style.display="block";
		$("new_right_container").className = "new_product_tabinfo_rt_active";
		$("sep_2").className = "seperator_normal";
		$("sep_1").className = "seperator_normal";
	}
}

function related_produts(productid,ProductUrl) {
	var strvalue = ProductUrl.split(".php");
	
	//var pars    = GLOBALPATH+'fashion'+strvalue[0]+'-'+'p'+'-'+productid+'.html';
	//window.top.location = 	GLOBALPATH+"fashion/strvalue[0]-p-"+productid;
	window.top.location = 	GLOBALPATH+'clothing/'+strvalue[0]+'-'+'pid'+'-'+productid;
}

function getlargeimage(imagepath,ident,name,price,description,product_id)	{
	document.getElementById('largeimage').src = GLOBALPATH+"clothing/product_details.php?pid="+ident;
	document.getElementById('largeimage').src  = GLOBALPATH+"products/looks/medium/"+imagepath;
	document.getElementById('largeimage').title  = name;
	document.getElementById('looks_description').innerHTML  = description;
	//document.getElementById('content_link').href = GLOBALPATH+"fashion/product_details.php?pid="+ident;
	//document.getElementById('image_name').innerHTML  = name;
	//document.getElementById('image_price').innerHTML = price;
	//document.getElementById('image_description').innerHTML = desc;
	
	var success = function(t){ getlargeimage_success(t);}
	var failure = function(t){ getlargeimage_failed(t,size,product_id);}

	var pars    = 'action=getproductdetails&product_id='+product_id;
	var url		= GLOBALPATH+"clothing/ajax/fashion_products.php";
	var myAjax  = new Ajax.Request(url, {method:'post',postBody:pars, onSuccess:success, onFailure:failure});
}

function getlargeimage_success(t) {
	var strvalue = t.responseText;
	$('looks_productscontent').innerHTML = strvalue;

}
function IsValid(Val,Label)
{
	if(trim(Val)=="")
	{
		alert(Label+" Should Not be Empty")
		return false;
	}
	return true;
}
function trim(str)
{
	/*while(''+Val.charAt(0)==' ')
	Val=Val.substring(1,Val.length);
	return Val*/
	return str.replace(/^\s*|\s*$/g,"");
}
function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

