// On change of the atribute values this function is called
var dropdowntochangedefaulttext;  // var used to hold the dafault text of the 2nd dropdown
function populatestockvalues(seq,value,dropdownseq)
{
	//alert(seq+value+dropdownseq);
	var dropdowntochange=0;  //this variable will have the sequence number of the dropdown whose options have to be cahged.
							// As when there are multiple defining attibute, teh dropdowns will be in teh form of array name attrValue_1[].
							// So this variable will identify excalty which dropdown varables need to be checked
	var dropdowntochangeprevvalues; // to store the original valuesof the dropdon
	if(dropdownseq==1) dropdowntochange=2;
	if(dropdownseq==2) dropdowntochange=1;
	if(dropdownseq=="") dropdowntochange=1;  // this condition occurs only when the 1st attribute is displyed as dropdown and the seconf attribute is hidden

	if(eval("document.OrderItemAddForm.attrValue_"+seq)[dropdowntochange-1].length!=null){
	//w= eval("document.OrderItemAddForm.attrValue_"+seq)[dropdowntochange-1].selectedIndex;
	dropdowntochangedefaulttext=eval("document.OrderItemAddForm.attrValue_"+seq)[dropdowntochange-1].options[0].text;
	}
	
	if(eval("document.OrderItemAddForm.attrValue_"+seq).length >1 && (dropdownseq==1 || dropdownseq=="")  && noofattrs[seq]>1 ) // This logic works only for multiple dropdowns and only if the first dropdow is selected
			{
			//alert('inside');			
			
			resetdropdowns(seq)
			dropdowntochangeprevvalues = new Array();
			var tmpvalue;
			//get the old values in the dropdown
			for(var t=0;t<eval("document.OrderItemAddForm.attrValue_"+seq)[dropdowntochange-1].length;t++)
				{
			//		alert(eval("document.OrderItemAddForm.attrValue_"+seq)[dropdowntochange-1].options[t].value);
					tmpvalue=eval("document.OrderItemAddForm.attrValue_"+seq)[dropdowntochange-1].options[t].value;
					// if the values had been previously cahged by teh script, the following loop will restore to origianl by getting a substing of the present value
					if(tmpvalue.indexOf("Out of stock")>1)  //identify if the value has the out of stock
					{	
						tmpvalue=tmpvalue.substring(0,tmpvalue.indexOf("-")-1); //if yes, truncate that part to get the original
					}
					//remove the out of stock from previous optiosn if its present
					//alert(tmpvalue);
					dropdowntochangeprevvalues[t]=eval("document.OrderItemAddForm.attrValue_"+seq)[dropdowntochange-1].options[t].value;
				}
			// chanfget the dropdown values
			fillList(eval("document.OrderItemAddForm.attrValue_"+seq)[dropdowntochange-1],dropdowntochangeprevvalues,value,seq);
		}
		
	//	if(eval("document.OrderItemAddForm.attrValue_"+seq)[dropdowntochange-1].length!=null && eval("document.OrderItemAddForm.attrValue_"+seq)[dropdowntochange-1].value=="" && dropdowntochangecurrtext.indexOf("choose")>=0)
	//	{
	//		eval("document.OrderItemAddForm.attrValue_"+seq)[dropdowntochange-1].options[w].text=dropdowntochangecurrtext;
	//	}
}


function fillList( box, prevalarr,valueselectedinotherdrop ,seq) 
{
	// arr[0] holds the display text
	// arr[1] are the values
	//alert('filllist');
	
	//the following if is required to avoid script errors when the second attribute has only one value and is displayed as 
	// text instead of dropdown
	if(box.length == null)return;
	else
		while ( box.options.length ) box.options[0] = null;
		for ( var i = 0; i < prevalarr.length; i++ ) {
			// Create a new drop down option with the
			// display text and value after comparing from the unavailable array
			if(checkstockfor(valueselectedinotherdrop,prevalarr[i],seq))
			{	//alert('in normal');
			
			
				if(prevalarr[i]=="")
					option = new Option(dropdowntochangedefaulttext ,prevalarr[i] );
				else
				option = new Option( prevalarr[i], prevalarr[i] );
			}
			else
			{ //if false meands the stock is not available. APpend the option with "-Out of stock" 
				// THE APPENDED PART SHOULD NOT BE CAHGED>>AS THE COMPLETE LOGIC IS DEPENDANT ON IT.
				// IF REQUIRED TO CAHNGE, THEN CAHNGE ALL TEH APPEARENCE OF " - Out of stock" with somethin like
				// " - NA" . THE INITIA SPACE AND NA ARE MANDATORY AND SHOULD NOT BE CAHGED. IF REQUIRES TO EVEN CHAGE THAT
				// TEHN THE LOGIC SHOULD BE CHANGED
				//alert('in out of stock');
			option = new Option( prevalarr[i]+" - Out of stock", prevalarr[i]+" - Out of stock" );
			}
			// Add to the end of the existing options
			box.options[box.length] = option;
		}
	// Preselect option 0
	//	box.selectedIndex=0;
}


// Resets both the dropdowns to roiginal values without the out of stock append.
// Uses javascript functions like indexof / substr. on " - Out of stock"
function resetdropdowns(seq)
{
	//alert('reset');
	var tmpvalue;
	for(var t=0;t<eval("document.OrderItemAddForm.attrValue_"+seq).length;t++)
		{
			for(var x=0;x<eval("document.OrderItemAddForm.attrValue_"+seq)[t].length;x++)
			{
				tmpvalue=eval("document.OrderItemAddForm.attrValue_"+seq)[t].options[x].value;
			//	alert("new logic fix here"+tmpvalue);
				if(tmpvalue.indexOf("Out of stock")>1)
				{	
				tmpvalue=tmpvalue.substring(0,tmpvalue.indexOf("- Out of stock")-1);
				}
				eval("document.OrderItemAddForm.attrValue_"+seq)[t].options[x].value=tmpvalue;
				
				
			}
		}
}


//COMPARES IF THE COMBINATION OF TEH PARAMETERS PASSED EXISTS IN THE OUTOFSTOCK ARRAY THAT IS CREATED AT THE BEGINNING
function checkstockfor(valueselectedinotherdrop,val,seq)
{
	var retval =true;
	//alert('checkstockfor');
	//alert(seq+ 'checkstockfor'+noofoutstockprods[seq]);
	for(var k=0;k<noofoutstockprods[seq];k++)
	{
		//alert("to check against>."+valueselectedinotherdrop +val)
		//alert(eval("attrarray"+seq)[k][0] +">>" +eval("attrarray"+seq)[k][1])
		//alert(val+ ">>"+valueselectedinotherdrop+ " values to check"+attrarray[k][0]+">>"+attrarray[k][1])
		if((eval("attrarray"+seq)[k][0]==val ||eval("attrarray"+seq)[k][1]== val) && (eval("attrarray"+seq)[k][0]==valueselectedinotherdrop ||eval("attrarray"+seq)[k][1]== valueselectedinotherdrop))
		{	
		//alert("checkstockfor in no stock loop");
		retval= false;
		break;
		}
		else
			retval= true;
	}
	return retval;

}


function loadstockValues(noofprods)
{
var disptext;
	for(var prodno=1;prodno<=noofprods;prodno++)
	{	
 		if(noofattrs[prodno]==1)
	{ 
		
	if(eval("document.OrderItemAddForm.attrValue_"+prodno)!=undefined ) //Added By Gopi Raman
	{
	
	if(eval("document.OrderItemAddForm.attrValue_"+prodno).length>1)
	{

		
		dropdowntochangeprevvalues = new Array();
		var tmpvalue;
			//get the old values in the dropdown
			for(var t=0;t<eval("document.OrderItemAddForm.attrValue_"+prodno).length;t++)
				{
					tmpvalue=eval("document.OrderItemAddForm.attrValue_"+prodno).options[t].value;
					dropdowntochangeprevvalues[t]=eval("document.OrderItemAddForm.attrValue_"+prodno).options[t].value;
	
					if(eval("document.OrderItemAddForm.attrValue_"+prodno).options[t].value=="")
					 disptext=eval("document.OrderItemAddForm.attrValue_"+prodno).options[t].text;
			}
		while ( eval("document.OrderItemAddForm.attrValue_"+prodno).options.length ) eval("document.OrderItemAddForm.attrValue_"+prodno).options[0] = null;
		
		for ( var i = 0; i < dropdowntochangeprevvalues.length; i++ ) {

			// Create a new drop down option with the
			// display text and value after comparing from the unavailable array
			
 		if(checkstocksingleattribute(dropdowntochangeprevvalues[i],prodno))
			{	
			
				if(dropdowntochangeprevvalues[i]=="")
					option = new Option( disptext ,dropdowntochangeprevvalues[i] );
				else
					option = new Option( dropdowntochangeprevvalues[i], dropdowntochangeprevvalues[i] );
			}
			else
			{ //if false meands the stock is not available. APpend the option with "-Out of stock" 
				// THE APPENDED PART SHOULD NOT BE CAHGED>>AS THE COMPLETE LOGIC IS DEPENDANT ON IT.
				// IF REQUIRED TO CAHNGE, THEN CAHNGE ALL TEH APPEARENCE OF " - Out of stock" with somethin like
				// " - NA" . THE INITIA SPACE AND NA ARE MANDATORY AND SHOULD NOT BE CAHGED. IF REQUIRES TO EVEN CHAGE THAT
				// TEHN THE LOGIC SHOULD BE CHANGED
			option = new Option( dropdowntochangeprevvalues[i]+" - Out of stock", dropdowntochangeprevvalues[i]+" - Out of stock" );
			}
			// Add to the end of the existing options
			eval("document.OrderItemAddForm.attrValue_"+prodno).options[eval("document.OrderItemAddForm.attrValue_"+prodno).length] = option;
		}		
	}
	eval("document.OrderItemAddForm.attrValue_"+prodno).selectedIndex=0;
	}
	}
	}
}


function checkstocksingleattribute(val,prodno)
{//alert(noofoutstockprods[prodno]+val+prodno);
	var retval =true;
	for(var k=0;k<noofoutstockprods[prodno];k++)
	{
	//alert(val+ ">>values to check"+eval("attrarray"+prodno)[k][0]+">>")
	if(eval("attrarray"+prodno)[k][0]==val)
	{	
	//alert("checkstockfor in no stock loop");
	retval= false;
	break;
	}
	else
		retval= true;
	}
	return retval;
}
///********************************STOCK CHECK SCRIPTS**********************************************************//



// This following function is used for the cros sell products.
// The catEntryId_seqno has to be null if the item is not to be added to the cart.
// So only when the user selects a quantity the catentry id should be assigned so that it will be added to the cart
// If this functionality is not implemented,then the server will go in a infinite loop. i.e if catentryid is available and quantity is 0

function assignCatentry(catentid,seq) {
	var field= eval("document.OrderItemAddForm.catEntryId_"+seq);
	if(eval("document.OrderItemAddForm.quantity_"+seq).value !=0) {
		field.value=catentid;
		} else {
		field.value="";
		}
}

function popup(content, title) { 
 	msgWindow = window.open('', 'window2','toolbar=0,scrollbars=0,location=no,top=170,left=300,statusbar=no,resizable=yes,width=450,height=200'); 
	msgWindow.document.write("<html><head><title>"+title+"</title></head><body><font face='arial'>"+content+"</font></body></html>"); 
	msgWindow.document.close; 
} 

function selectcolor(attrname,attrvalue,dropdownseq) {
	if(eval("document.OrderItemAddForm.attrName_1").length!=null ) {
		for(var x=0;x<document.OrderItemAddForm.attrValue_1.length;x++) {
		 if(document.OrderItemAddForm.attrValue_1[x].length>1){ // this if is to avoid script error one when either of the attribute is single valued and not displayed. 
		 														// it will be as a hidden field. So document.OrderItemAddForm.attrValue_1[x].options.length will give script error
			for(var z=0;z<document.OrderItemAddForm.attrValue_1[x].options.length;z++) {
				if(document.OrderItemAddForm.attrValue_1[x].options[z].value==attrvalue)
					document.OrderItemAddForm.attrValue_1[x].selectedIndex=z;
		}
		}
	populatestockvalues('1',attrvalue,dropdownseq)	
	}	
	// when multiple attributes are present
 } else { //when single attribute is present
	 for(var z=0;z<document.OrderItemAddForm.attrValue_1.options.length;z++) {
		if(document.OrderItemAddForm.attrValue_1.options[z].value==attrvalue)
					document.OrderItemAddForm.attrValue_1.selectedIndex=z;
		}
	 }
}

var newwindow;
var newwindow1;
function openPopupImg(imgName, imgPath, title) {
	var path = imgPath + imgName ;
	if (newwindow && !newwindow.closed) {
		newwindow.focus(); 
		newwindow.document.clear() 
	} else { 
		newwindow=window.open('','','width=745,height=602, resizable=0,scrollbars=0') }
		newwindow.document.open();
		newwindow.document.writeln('<html> <head> <title>');
		newwindow.document.writeln("Debenhams -"+title+" ");
		newwindow.document.writeln('<\/title> <\/head> <body topmargin="0" ><center><table width="745" border="0" cellpadding="0" cellspacing="0">');
		newwindow.document.writeln('<tr><td width="65" height="400">&nbsp;</td><td rowspan=2 align="right"><img src=' + path +"_XLG.JPG	" + ' alt=\"' + imgName +"_XLG.JPG" + '\" height="600" width="595" >');
		newwindow.document.writeln('</td><td width=65  align="left" valign="bottom"><input type ="button" onmouseout="this.style.color=\'#FFFFFF\';" onmouseover="this.style.color=\'#CCCCCC\';" onclick="window.close()" value="CLOSE" STYLE="height:20px;width: 65px;background:#555555;font-family:Verdana;font-size: 0.7em;font-weight: bold;color: #FFFFFF;text-align : centre;text-decoration: none;border: thin;cursor: hand;"  >&nbsp;&nbsp;</td><td width="5">&nbsp;</td>	</tr>');
		newwindow.document.writeln('<tr><td height="10" colspan="4"> </td></tr>');
		newwindow.document.writeln('</table><\/center> <\/body> <\/html>');	
		newwindow.document.close();
}

function showmoreImg(no, imgname, imgpath, title)
{
	if (newwindow1 && !newwindow1.closed) {
			newwindow1.focus(); 
			newwindow1.document.clear() 
		} else {
		newwindow1=window.open('','','width=415, resizable=1,scrollbars=1') 
		newwindow1.document.writeln('<html> <head> <title>');
		newwindow1.document.writeln("Debenhams -"+title+" ");
		newwindow1.document.writeln('</title></head> <body topmargin="0" style="left: 0mm;"><center><table width="375" border="0" cellpadding="0" cellspacing="0">');
		newwindow1.document.writeln('<tr><td height="15" colspan="3"> <spacer type="blank" height="1"></td></tr>');
	for (var count=1;count<=no;count++)	{
		var path=imgpath+imgname+"_"+count ;
		newwindow1.document.writeln('<tr><td width="12" height="300" ><spacer type="blank" width="12" ></td><td> <img src=' + path +"_LRG.JPG" + ' alt=\"' + imgname + '\" align="right" height="348" width="348" border="1">');
		newwindow1.document.writeln('</td><td width="12" ></td></tr>');
		
		newwindow1.document.writeln('<tr><td height="16" colspan="3">&nbsp;</td></tr>');
	}
		newwindow1.document.writeln('<tr><td height="16"><td align="right"> <input type ="button" onmouseout="this.style.color=\'#FFFFFF\';" onmouseover="this.style.color=\'#CCCCCC\';" onclick="window.close()" value="CLOSE" STYLE="height:20px;width: 65px;background:#555555;font-family:Verdana;font-size: 0.7em;font-weight: bold;color: #FFFFFF;text-align : centre;text-decoration: none;border: thin;cursor: hand;"  >&nbsp;</td><td> </td></tr>');
		newwindow1.document.writeln('</table><\/center> <\/body> <\/html>');	
	
	}
}