var xPos;
var yPos;

function updateOptionPrice(frm,selectid,optname){	
	//alert(optname)
	if(document.getElementById("vwquantity0") && (document.getElementById("vwquantity0").value > 1 || optname.indexOf("vwquantity")>-1)){
		updateQtyOptionPrice(frm,selectid,optname)
		}else{
			//alert(selectid);
	orderForm = frm;
	pp = document.getElementById("prodPrice").value;
	document.getElementById("optionPrice").innerHTML = pp;
	actual_price = pp.substr(pp.indexOf("$")+1, pp.length);
	//qtyName = "frm."+selectid.replace("group","vwquantity")+".value";
	//qtyValue = eval(qtyName);
	//alert(selectid);
	var price = parseFloat(actual_price);
	var pricevalAr = new Array();
	var direction = "add";
	var	qtyPrice=0; 	
		
	//alert(pp);
		
	for(i=0; i< frm.elements.length; i++){
				e = frm.elements[i];
				var itemId=e.id;
				var optionName=e.name;
				var qtyId = itemId.replace("group","vwquantity");
				var qtyNo = qtyId.replace("vwquantity","");
				var selectNo = itemId.replace("group","");				
				var defQty=1;
				if(e.type=="select-one"){
				var selIndex = document.getElementById(itemId).selectedIndex;				
				var selText = document.getElementById(itemId).options[selIndex].text;
				//alert("Qty id: " + qtyNo + " Goup Id: " + selectNo + " optionName: "+ selText);
				//alert(selText);
				priceval = selText;
				//alert("qtyNo: " + Number(qtyNo) + " selectno: "+Number(selectNo));
				if(qtyNo && selectNo && (qtyNo==selectNo)){
					//alert("Text: " + priceval + " Pos: " +priceval.indexOf("(+$"));
					if(priceval.indexOf("(+$") > -1) {
						pricevalAr = priceval.split("(+$");
						direction = "add";
					} else if(priceval.indexOf("(+") > -1) {
						pricevalAr = priceval.split("(+");
						direction = "add";
					} else if(priceval.indexOf("(-") > -1) {
						pricevalAr = priceval.split("(-");
						direction = "sub";
					}else if(priceval.indexOf("(-$") > -1) {
						pricevalAr = priceval.split("(-$");
						direction = "sub";
					}else {
						pricevalAr = "";
					}
	
					if(pricevalAr.length > 1)
					{
						priceval = pricevalAr[1].split(")");
						priceval = parseFloat(priceval[0]);						
					}
					else{
						priceval = 0;
					}
					
					//alert(pricevalAr.length);
					if(direction == "add"){
						if(document.getElementById(qtyId)){
								defQty = document.getElementById(qtyId).value;
								price = price + (priceval*defQty);
							}else {
								price = price + (priceval*defQty);
							}					
					}else if(direction == "sub")
						price = price - priceval;
						document.getElementById("optionPrice").innerHTML = formatMoney(price,"$",",",".");			
						
					}		
				}
	}
			}
	
}

function updateQtyOptionPrice(frm,opId,opName){	
	proPrice = document.getElementById("prodPrice").value;
	proPrice = document.getElementById("vwquantity0").value * proPrice;	
	var currentPrices = findQtyPrice(frm,opName)
	//alert(currentPrices);
	var additionaPrices = findAdditionalQtyPrice(frm,opName);
	//alert(additionaPrices);
	document.getElementById("optionPrice").innerHTML = formatMoney(currentPrices+additionaPrices,"$",",",".");
	}

function findAdditionalQtyPrice(frm,oname){
	var finalprice = 0;
	var optpriceAry = new Array();	
	var defQty =1;
	for(j=0; j<frm.elements.length; j++){
				e = frm.elements[j];
				var itemId=e.id;
				var qtyId = itemId.replace("group","vwquantity");
				var optionName=e.name;
				var itemQty=1;
				if(e.type=="select-one"){
					var selIndex = document.getElementById(itemId).selectedIndex;				
					var selText = document.getElementById(itemId).options[selIndex].text;
					optpriceval = selText;			
				
					if(optionName && optionName.indexOf("vwitem1")>-1){
							//alert(optionName);
							if(optpriceval.indexOf("(+$") > -1) {
								optpriceAry = optpriceval.split("(+$");
								direction = "add";
							}else if(optpriceval.indexOf("(+") > -1) {
								optpriceAry = optpriceval.split("(+");
								direction = "add";
							}else if(optpriceval.indexOf("(-") > -1) {
								optpriceAry = optpriceval.split("(-");
								direction = "sub";
							}else if(optpriceval.indexOf("(-$") > -1) {
								optpriceAry = optpriceval.split("(-$");
								direction = "sub";
							}else {optpriceAry = "";}						
					
					if(optpriceAry.length > 1){
								optpriceval = optpriceAry[1].split(")");
								optpriceval = parseFloat(optpriceval[0]);
							}else{
								optpriceval = 0;
							}
					if(document.getElementById(qtyId)){
							defQty = document.getElementById(qtyId).value;
							finalprice = finalprice + (optpriceval*defQty);
						}else {
							finalprice = finalprice + (optpriceval*defQty);
						}
					//finalprice = finalprice + optpriceval;					
					}
				}
					
				}

				//alert(finalprice)	
				return finalprice;				
						}	


function findQtyPrice(frm,oname,direction){
	//alert(frm);
	salePrice = document.getElementById("prodPrice").value;
	//salePrice = 0;
	var finalprice = parseFloat(salePrice);
	var optpriceAry = new Array();
	//alert(salePrice);
	
	for(j=0; j<frm.elements.length; j++){
				e = frm.elements[j];
				var itemId=e.id;
				var optionName=e.name;
				var itemQty=1;
				if(e.type=="select-one"){
					var selIndex = document.getElementById(itemId).selectedIndex;				
					var selText = document.getElementById(itemId).options[selIndex].text;
					optpriceval = selText;			
				
					if(optionName && optionName.indexOf("vwattr0")>-1){
							//alert(optionName);
							if(optpriceval.indexOf("(+$") > -1) {
								optpriceAry = optpriceval.split("(+$");
								direction = "add";
							}else if(optpriceval.indexOf("(+") > -1) {
								optpriceAry = optpriceval.split("(+");
								direction = "add";
							}else if(optpriceval.indexOf("(-") > -1) {
								optpriceAry = optpriceval.split("(-");
								direction = "sub";
							}else if(optpriceval.indexOf("(-$") > -1) {
								optpriceAry = optpriceval.split("(-$");
								direction = "sub";
							}else {optpriceAry = "";}						
					
					if(optpriceAry.length > 1){
								optpriceval = optpriceAry[1].split(")");
								optpriceval = parseFloat(optpriceval[0]);
							}else{
								optpriceval = 0;
							}
					
					if(direction == "add") finalprice = finalprice + optpriceval;					
					}
									
					}
					
					
					
				}
				if(document.getElementById("vwquantity0")){
						finalprice = finalprice * document.getElementById("vwquantity0").value;
					}
				//alert(finalprice)	
				return finalprice;				
						}	
 

 
function showToolTip(selectGroup,tt){
	
//	onmouseover="showToolTip('Link tip','This is a link tip demo.',event);" onmouseout="hideToolTip();"
	var page=document.getElementById(selectGroup).value+".html";
	page = page.toLowerCase();
	var tipIdno = selectGroup.replace("group","");
	var tipId = "inicTips"+tipIdno;
	var tipsIframe = '<iframe frameborder="0" scrolling="no" align="bottom" marginheight="0" marginwidth="0" src="javascript:\'\';" style="position:absolute; left:0; top:0px; width:350px; display:block; filter:alpha(opacity=0);"></iframe>';
	document.getElementById(tt).innerHTML = "";	
	document.getElementById(tt).style.display = "block";
//	$(tt).show("slow");
	document.getElementById(tt).style.visibility = "visible";
//	$(tt).hide(500).fadeIn(500);


	tipsDataDefault = tipsIframe+"<h1 class='tooltip-title'>Loading...</h1>";
	tipsDataDefault += "<center class='loading'><img src='http://lib.store.yahoo.net/lib/yhst-85845721015556/loading.gif' alt='Loading' style='display:block; align:center;'>Please wait... loading data</center>";


	document.getElementById(tt).innerHTML = tipsDataDefault;

	var tipsData;
	var selIndex = document.getElementById(selectGroup).selectedIndex;	
	var selText = document.getElementById(selectGroup).options[selIndex].text;
	var tipsIframe = '<iframe frameborder="0" scrolling="no" align="bottom" marginheight="0" marginwidth="0" src="javascript:\'\';" style="position:absolute; left:0; top:0px; width:350px; display:block; filter:alpha(opacity=0);"></iframe>';
	//alert("Id: " + page+ " Text: "+tipId);
	if(page!="" && page!=".html"){			
		$.ajax({
				type: "GET",
				url: page,
				cache: false,
				success: function(data){
					itemImgTag = data.split("<!--itemImgStart-->");
					itemDescTag = data.split("<!--DescStart-->");
					if((itemImgTag[1] && itemImgTag[1]!="") || (itemDescTag[1] && itemDescTag[1] !="")){
							itemImg = itemImgTag[1].split("<!--itemImgEnd-->");
							itemImg = itemImg[0]					
							
							itemDesc = itemDescTag[1].split("<!--DescEnd-->");
							itemDesc = itemDesc[0];
							
							tipsData ="<span class='tooltip-title'>"+selText+"</span>";
							tipsData += "<p class='ttp'>";
							if(itemImg && itemImg!=""){
								tipsData += "<img src='"+itemImg+"' align='left' style='float:left; display:inline-block; padding:5px;'>";
							}
							tipsData += "<span>"+itemDesc+"</span></p>";					
					}else{
						  tipsData ="<span class='tooltip-title'>No data found..</span>";
							tipsData += "<center>Sorry no data found...!";
						}		
					document.getElementById(tt).innerHTML = tipsIframe+tipsData;

					//var tipsData = tipsIframe+tipsData;
					//alert(tipsData)
				}
			});
	}else{
			var toolTip = document.getElementById(tt);
		   var tipsIframe = '<iframe frameborder="0" scrolling="no" align="bottom" marginheight="0" marginwidth="0" src="javascript:\'\';" style="position:absolute; left:0; top:0px; width:320px; display:block; filter:alpha(opacity=0);"></iframe>';

		  toolTip.innerHTML = tipsIframe + "<h1 class='tooltip-title'>"+ttitle+"</h1><p class='ttp'>Please select the option and move cursou here for help</p>";
	}	

}




function showToolTip1(title,msg,tt){

   var toolTip = document.getElementById(tt);
   var tipsIframe = '<iframe frameborder="0" scrolling="no" align="bottom" marginheight="0" marginwidth="0" src="javascript:\'\';" style="position:absolute; left:0; top:0px; width:320px; display:block; filter:alpha(opacity=0);"></iframe>';

   toolTip.innerHTML = tipsIframe + "<h1 class='title'>"+title+"</h1><p class='ttp'>"+msg+"</p>";
//   toolTip.style.top = parseInt(yPos)+50 + "px";
  // toolTip.style.left = parseInt(xPos)+20 + "px";
//  	top = 233 Left 946
//  	alert("Top: "+ parseInt(yPos) + " Left: " + parseInt(xPos));
   //toolTip.style.top = "420px";
//	toolTip.style.top = parseInt(yPos)+"px";
//	toolTip.style.left = parseInt(xPos) + "px";
  // toolTip.style.left = "630px";
   toolTip.style.visibility = "visible";
   
}

function hideToolTip(tt){
   var toolTip = document.getElementById(tt);
   $(toolTip).fadeOut("medium");
   //toolTip.style.visibility = "hidden";
}

function toDollarsAndCents(n) {
  var s = "" + Math.round(n * 100) / 100
  var i = s.indexOf('.')
  if (i < 0) return s + ".00"
  var t = s.substring(0, i + 1) + s.substring(i + 1, i + 3)
  if (i + 2 == s.length) t += "0"

  return Number(t,2)
}

function isThousands(position) {
if (Math.floor(position/3)*3==position) return true;
return false;
};

function formatMoney (theNumber,theCurrency,theThousands,theDecimal) {
var theDecimalDigits =
Math.round((theNumber*100)-(Math.floor(theNumber)*100));
theDecimalDigits= ""+ (theDecimalDigits + "0").substring(0,2);
theNumber = ""+Math.floor(theNumber);
var theOutput = theCurrency;
for (x=0; x<theNumber.length; x++) {
theOutput += theNumber.substring(x,x+1);
if (isThousands(theNumber.length-x-1) && (theNumber.length-x-1
!=0)) {
theOutput += theThousands;
};
};
theOutput += theDecimal + theDecimalDigits;
return theOutput;
};

