var htmltooltip={
	tipclass: 'htmltooltip',
	fadeeffect: [true, 500],
	anchors: [],
	tooltips: [], //array to contain references to all tooltip DIVs on the page

	positiontip:function($, tipindex, e){
		var anchor=this.anchors[tipindex]
		var tooltip=this.tooltips[tipindex]
		var scrollLeft=window.pageXOffset? window.pageXOffset : this.iebody.scrollLeft
		var scrollTop=window.pageYOffset? window.pageYOffset : this.iebody.scrollTop
		var docwidth=(window.innerWidth)? window.innerWidth-15 : htmltooltip.iebody.clientWidth-15
		var docheight=(window.innerHeight)? window.innerHeight-18 : htmltooltip.iebody.clientHeight-15
		var tipx=anchor.dimensions.offsetx + 30
		var tipy=anchor.dimensions.offsety+anchor.dimensions.h + 15
		tipx=(tipx+tooltip.dimensions.w-scrollLeft>docwidth)? tipx-tooltip.dimensions.w : tipx //account for right edge
		tipy=(tipy+tooltip.dimensions.h-scrollTop>docheight)? tipy-tooltip.dimensions.h-anchor.dimensions.h : tipy //account for bottom edge
		$(tooltip).css({left: tipx, top: tipy})
	},

	showtip:function($, tipindex, e){
		var tooltip=this.tooltips[tipindex]
		if (this.fadeeffect[0])
			$(tooltip).hide().fadeIn(this.fadeeffect[1])
		else
			$(tooltip).show()
	},

	hidetip:function($, tipindex, e){
		var tooltip=this.tooltips[tipindex]
		if (this.fadeeffect[0])
			$(tooltip).fadeOut(this.fadeeffect[1])
		else
			$(tooltip).hide()	
	},

	updateanchordimensions:function($){
		var $anchors=$('*[@rel="'+htmltooltip.tipclass+'"]')
		$anchors.each(function(index){
			this.dimensions={w:this.offsetWidth, h:this.offsetHeight, offsetx:$(this).offset().left, offsety:$(this).offset().top}
		})
	},

	render:function(){
		jQuery(document).ready(function($){
			htmltooltip.iebody=(document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
			var $anchors=$('*[@rel="'+htmltooltip.tipclass+'"]')
			var $tooltips=$('div[@class="'+htmltooltip.tipclass+'"]')
			$anchors.each(function(index){ //find all links with "title=htmltooltip" declaration
				this.dimensions={w:this.offsetWidth, h:this.offsetHeight, offsetx:$(this).offset().left, offsety:$(this).offset().top} //store anchor dimensions
				this.tippos=index+' pos' //store index of corresponding tooltip
				var tooltip=$tooltips.eq(index).get(0) //ref corresponding tooltip
				if (tooltip==null) //if no corresponding tooltip found
					return //exist
				tooltip.dimensions={w:tooltip.offsetWidth, h:tooltip.offsetHeight}
				$(tooltip).remove().appendTo('body') //add tooltip to end of BODY for easier positioning
				htmltooltip.tooltips.push(tooltip) //store reference to each tooltip
				htmltooltip.anchors.push(this) //store reference to each anchor
				var $anchor=$(this)
				$anchor.hover(
					function(e){ //onMouseover element
						htmltooltip.positiontip($, parseInt(this.tippos), e)
						htmltooltip.showtip($, parseInt(this.tippos), e)
					},
					function(e){ //onMouseout element
						htmltooltip.hidetip($, parseInt(this.tippos), e)
					}
				)
				$(window).bind("resize", function(){htmltooltip.updateanchordimensions($)})
			})
		})
	}
}

htmltooltip.render()



function updateOptionPrice(frm,selectid,optname){	
	
	if(document.getElementById("vwquantity0") && (document.getElementById("vwquantity0").value > 1 || optname.indexOf("vwquantit"))){
		updateQtyOptionPrice(frm,selectid,optname)
		}else{
			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);
				priceval = selText;
				//alert(selText);
				if((Number(qtyNo) && Number(selectNo)) && (qtyNo==selectNo)){
					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;
					}
					
					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 = "$" + toDollarsAndCents(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 = "$" + toDollarsAndCents(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 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 t
}

 
 
function setItemData(selectGroup){
	
	var page=document.getElementById(selectGroup).value+".html";
	page = page.toLowerCase();
	var tipIdno = selectGroup.replace("group","");
	var tipId = "inicTips"+tipIdno;
	//alert(tipId);
	//alert(page);
	document.getElementById(tipId).innerHTML = "";
	tipsDataDefault ="<span class='title'>Loading...</span>";
	tipsDataDefault += "<p align='center' class='loading'>Please wait... loading data</p>";
	document.getElementById(tipId).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){
					//alert(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='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='title'>No data found..</span>";
							tipsData += "<center>Sorry no data found...!";
						}		
					document.getElementById(tipId).innerHTML = tipsIframe+tipsData;
					//alert(document.getElementById(tipId).innerHTML)
				}
			});
	}else{
			//MyTip("Custom Message","Please select the Option and move cursor here for help")			
			tipsData ="<span class='title'>"+ttitle+"</span><p class='ttp'>Please select the Option and move cursor here for help</p>";
			document.getElementById(tipId).innerHTML = tipsIframe+tipsData;
	}		
}
