var xmlHttp;
var OptionCode;
var strRequestProcessing = "";
var intTOtalNumberOfItem; 
var intxPosition , intYPosition , divIdCode;
var myObj;
var strDestinationObject = "";
var randomimages=new Array();
var delay=5000;
var curindex=0;

window.addEvent('domready', function() {
	myObj = new MyRotator();
	myObj.init();
})

strRequestProcessing="<TABLE BORDER='0' WIDTH='60%'><TR><TD ALIGN='CENTER'><IMG SRC='./images/progress-loader.gif'></TD></TR></TABLE>";

function doOpreation(strDestinationURL,optionCodePass)
{ 
	xmlHttp=GetXmlHttpObject();
	OptionCode = optionCodePass;
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	 }
	var url="";
	url=strDestinationURL;
	//if(optionCodePass == 8)
	//	xmlHttp.onreadystatechange=stateChangedCart
	//else
	if(optionCodePass == -1)
		xmlHttp.onreadystatechange=stateChangedOfQuantity
	else if(optionCodePass < 0 )
		xmlHttp.onreadystatechange=stateChangedOnApplcation
	else
		xmlHttp.onreadystatechange=stateChanged
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChanged()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=='complete')
	{
		var strRerurnString = xmlHttp.responseText;
 		strRerurnString = strRerurnString.split('~~~');

		document.getElementById('items').innerHTML= strRerurnString[0];
		document.getElementById('minitotal').innerHTML="&pound;"+strRerurnString[1];
		document.getElementById('minisubtotal').innerHTML="&pound;"+strRerurnString[1];
		document.getElementById('miniitems').innerHTML=strRerurnString[2];		
	}
}

function stateChangedOfQuantity()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=='complete')
	{
		var strRerurnString = xmlHttp.responseText;
 		strRerurnString = strRerurnString.split('~~~');
 
		document.getElementById(strDestinationObject).innerHTML= strRerurnString[0];
		document.getElementById('minitotal').innerHTML="&pound;"+strRerurnString[1];
		document.getElementById('minisubtotal').innerHTML="&pound;"+strRerurnString[1];
		document.getElementById('miniitems').innerHTML=strRerurnString[2];		
	}
}


/*************************************************************************************************************************************************
Purpose : Creating XMLHTTPRequest Object do perfrom the XMLHTTP communcation.
Input   : None.
Return  : XMLHTTPRequest Object.
*************************************************************************************************************************************************/
function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
		{
			xmlHttp=new XMLHttpRequest();
		}
    catch (e)
		{
			 try
			  {
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			  }
		 catch (e)
			  {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			  }
		}
	return xmlHttp;
}

/*************************************************************************************************************************************************
Purpose : Remove the space.
Input   : Nobe.
Return  : Overrides the existing string trim function.
*************************************************************************************************************************************************/
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

/*************************************************************************************************************************************************
Purpose : Email address validation.
Input   : email addresss = [ strEmailAddress ].
Return  : true = valid email address , false = invalid email address.
*************************************************************************************************************************************************/
function validateEmailAddress(strEmailAddress)
{
	var str=strEmailAddress;
	var filter=/^.+@.+\..{2,3}$/

	if (filter.test(str))
			return(true);
	else 
		return(false); 
}
 
/*************************************************************************************************************************************************
Purpose : Adding item in the shopping cart. if session is not exists then simply redirect user to login the system.
Input   : ProductCode = [ pProductCode ],
		  User Code = [ userCode ].
Return  : None.
*************************************************************************************************************************************************/
function addingIetmInShoppingCart(pProductCode,userCode,pQuantity)
{
	doOpreation("./ajaxOpreation.php?opreation=8&userCode="+userCode+"&productCode="+pProductCode+"&productQuantity="+document.getElementById(pQuantity).value,8);
}

/*************************************************************************************************************************************************
Purpose : Calculation of the item row price.
Input   : pQuantity = [ Selected Quantity ],
		  pUnitPrice = [ Product Unit Price ], 
		  pRowTotal = [ Row Total ].
Return  : product sum.
*************************************************************************************************************************************************/
function calculateRowPrice(pQuantity , pUnitPrice , pRowTotal )// , pSubTotal , pShippingCharges , pDiscountAmount , pGrandTotal)
{
	document.getElementById(pRowTotal).innerHTML = "&pound; " + (pQuantity * getCost(document.getElementById(pUnitPrice).innerHTML)).toFixed(2);
	calculateShoppingCartPrice(document.getElementById('txtNumberOfItem').value);
}

/*************************************************************************************************************************************************
Purpose : Calculating Shopping cart Footer.
Input   : Total number of items = [ pTotalnumberOfItem ].
Return  : Shopping Cart Footer Sum.
*************************************************************************************************************************************************/
function calculateShoppingCartPrice(pTotalnumberOfItem)
{
	var strRowTotal , dblSubTotal , dblShippingAmount , dblDiscountAmount , dblGrandTotal , dblShowFooter;
	 
	dblSubTotal = 0;
	dblGrandTotal = 0
	dblShowFooter = document.getElementById("txtShowFooter").value;
	if(pTotalnumberOfItem == "")
		intTOtalNumberOfItem = document.getElementById('txtNumberOfItem').value;
	else
		intTOtalNumberOfItem = pTotalnumberOfItem;
	
	for(var intCounterForLoop = 1 ; intCounterForLoop <= intTOtalNumberOfItem; intCounterForLoop++)
	{
		strRowTotal = "divTotal"+intCounterForLoop;
		dblSubTotal = dblSubTotal + parseFloat(getCost(document.getElementById(strRowTotal).innerHTML));
	}
	
	document.getElementById('divSubTotal').innerHTML = "&pound; " + dblSubTotal.toFixed(2);

	if(intTOtalNumberOfItem == -1)
	{
		dblSubTotal = getCost(document.getElementById('divSubTotal').innerHTML);

		if(document.getElementById('divShippingCharges').innerHTML == "")
			{
				document.getElementById('divShippingCharges').innerHTML = "&pound 0.00";
				dblShippingAmount = 0;
			}
		else
			dblShippingAmount  = getCost(document.getElementById('divShippingCharges').innerHTML);

		if(document.getElementById('divDiscountAmount').innerHTML == "")
			{
				document.getElementById('divDiscountAmount').innerHTML = "&pound 0.00";
				dblDiscountAmount = 0;
			}
		else
			dblDiscountAmount = getCost(document.getElementById('divDiscountAmount').innerHTML);

	 
			dblGrandTotal = ((parseFloat(dblSubTotal) + parseFloat(dblShippingAmount)) - parseFloat(dblDiscountAmount)) ;
	
		document.getElementById('divGrandTotal').innerHTML = "&pound; " + (dblGrandTotal).toFixed(2);
	}
}

/*************************************************************************************************************************************************
Purpose : Getting nueric value from string.
Input   : pValue = [ passed value ].
Return  : Numeric digit.
*************************************************************************************************************************************************/
function getCost(pValue)
{
	var strCost;
	strCost = pValue;
	return((strCost.substring(2)).trim());
}
/*************************************************************************************************************************************************
Purpose : Adding selected item in shopping cart.
Input   : pProductCode = [ Product Code ],
	      pQuantyty = [ Quantity ],
		  pDivObject  = [ Div Object Id to display the message ]
Return  : Numeric digit.
*************************************************************************************************************************************************/
function addItemInShoppingCart(pProductCode , pQuantity , pDivObject)
	{
		divIdCode = pDivObject;
		doOpreation("./ajaxOpreation.php?opreation=8&productCode="+pProductCode+"&productQuantity="+pQuantity,2);
	}
function addItemInShoppingCartOther(pProductCode , pQuantity , pDivObject)
	{
		divIdCode = pDivObject;
		doOpreation("./ajaxOpreation.php?opreation=8&productCode="+pProductCode+"&productQuantity="+pQuantity+"&pDesgin=1",2);
	}
function setOtherOrderPanel()
{
		var strProductID;
		var intTotalNumberOfElement = (document.getElementById('hyudsyudsbc6484dchyff').value);
 
		for(var intCounter = 1 ; intCounter < intTotalNumberOfElement ; intCounter++)
		{
			strProductID = "divConformation"+intCounter;
			
			if( divIdCode != strProductID)
				document.getElementById(strProductID).style.display="none";
			else
				document.getElementById(strProductID).style.display="block";
		}
}
function displayFloatingDiv(divId, title, width, height, left, top)
{
    DivID = divId;

    document.getElementById('dimmer').style.visibility = "visible";

    document.getElementById(divId).style.width = width + 'px';
    document.getElementById(divId).style.height = height + 'px';
    document.getElementById(divId).style.left = left + 'px';
    document.getElementById(divId).style.top = top + 'px';

    var addHeader , originalDivHTML;

    if (originalDivHTML == "")
        originalDivHTML = document.getElementById(divId).innerHTML;

    addHeader = '<table style="width:' + width + 'px" class="floatingHeader">' +
                '<tr><td ondblclick="void(0);" onmouseover="over=true;" onmouseout="over=false;" style="cursor:move;height:18px" class="normal_text">' + title + '</td>' +
                '<td style="width:18px" align="right"><a href="javascript:hiddenFloatingDiv(\'' + divId + '\');void(0);">' +
                '<img title="Close..." alt="Close..." src="images/close.jpg" border="0" /</a></td></tr></table>';


    // add to your div an header
    document.getElementById(divId).innerHTML = addHeader + originalDivHTML;


    document.getElementById(divId).className = 'dimming';
    document.getElementById(divId).style.visibility = "visible";
}

function handleEvent(oEvent) {
    intxPosition  = oEvent.screenX;
	intYPosition   = oEvent.screenY;	 
}

function showImage(pProductCode , pImageCounter)
{
	window.open("galleryImage.php?productCode="+pProductCode+"&imagecounter="+pImageCounter,"","location=0,status=1,scrollbars=0,width=800,height=650,resizable=0");
}

function setMenu(pObjetcCode, pAction)
{
	document.getElementById(pObjetcCode).style.display = pAction;
	
	if(pObjetcCode == "divSearch")
		window.setTimeout("hideSearch()", (15 * 1000));
}
function hideSearch()
{
	document.getElementById("divSearch").style.display = "none";
}
function checkingForSearchObject()
{
	var strSearchValue ;
	strSearchValue = document.getElementById("txtSearch").value;
	if(strSearchValue.trim() == "")
	{
		return false;
	}
}

function validateEmailAddress(pStrEmailAddress) 
	{
   		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	    var address = pStrEmailAddress;
   		if(reg.test(address) == false) 
		{
      		return false;
   		}
		else
		{
			return true;
		}		
	}	
function addItemToCart(pProductCode,pQuantity)
{
	doOpreation("http://www.reve.co.uk/ajaxOpreation.php?opreation=1&productCode="+pProductCode+"&productQuantity="+pQuantity,1);
	scroll(0,0);
	try
		{
 			Mini.show();
		}
	catch(err)
		{
			document.getElementById("minicart").className="minicart_open";
			document.getElementById("minicart").style.display = "block";
			document.getElementById("minicontent").style.display = "block";
			document.getElementById("items").style.display = "block";			
		}		   
	return false;
}
function showMiniCart()
{
	doOpreation("http://www.reve.co.uk/ajaxOpreation.php?opreation=2",1);
	return false;
}
function setShoppingCartQuantity(pProductCode,pDestinationCode,pQuantity,pCurrentStock)
{
	strDestinationObject = pDestinationCode;
	if(pQuantity == 2)
		{
 			var intQuantity = document.getElementById(pDestinationCode).innerHTML;
			if(parseInt(intQuantity) == 1)
 				{
					var strObjectCode = "item_"+pProductCode;
					document.getElementById(strObjectCode).style.display = 'none';
				}
			doOpreation("http://www.reve.co.uk/ajaxOpreation.php?opreation=3&productCode="+pProductCode+"&productQuantity="+pQuantity,-1);
			
		}
	else
 		doOpreation("http://www.reve.co.uk/ajaxOpreation.php?opreation=3&productCode="+pProductCode+"&productQuantity="+pQuantity,-1);
		
	return false;
}
function showBannerImages()
{
	var strImageList = document.getElementById("txtProductImage").value;
	randomimages = strImageList.split("~~~");	
}

function rotateimage()
{
 	document.images.defaultimage.src=randomimages[curindex];
	selectTab(curindex);
	if(curindex >= (randomimages.length -1))
		curindex = 0;
	else
		curindex++;
}

function showBanner(pIndex)
{
	document.images.defaultimage.src=randomimages[pIndex];
	selectTab(pIndex);
	curindex = pIndex;
	if(curindex >= (randomimages.length -1))
		curindex = 0;
	else
		curindex++;
	return false;
}

function selectTab(pIndex) {
		var selected;
		var li = document.getElementById('spotlight_tabs').getElementsByTagName('li');
		for (var i = 0; i < li.length; i++) {
			selected = '';
			if (li[i].getAttribute('id') == 'item_' +pIndex) {
				selected = 'selected';
			}
			li[i].className = selected;
		}
	}
