var flag;
var a = new Image();
var b = new Image();
var loader= new Image(16,16);
loader.src="http://lib.store.yahoo.net/lib/yhst-17159114447480/ajax-loader.gif"; 
//a.src = "http://lib.store.yahoo.net/lib/test-st51-indianic/bid-purchase-butt.gif";
//b.src = "http://lib.store.yahoo.net/lib/test-st51-indianic/addtocart-butt.jpg";
var temp;
function init(){
var sel = document.getElementById('selectBid');
//a.src = "http://lib.store.yahoo.net/lib/test-st51-indianic/bid-purchase-butt.gif";
//b.src = "http://lib.store.yahoo.net/lib/test-st51-indianic/addtocart-butt.jpg";
if(sel){
sel.options[0].selected = true;
}
//document.getElementById('divBid').style.display = "none";
//document.getElementById('divBid').style.visibility = "hidden";
 //btn.src = "http://lib.store.yahoo.net/lib/test-st51-indianic/addtocart-butt.jpg";
if(document.getElementById('imgButton')){
//document.getElementById('imgButton').style.visibility="hidden";
	//document.getElementById('imgButton').style.display="none";
}
}
function getVal(val,nam,frm){

for(i=0;i<frm.elements.length;i++){
   if(frm.elements[i].type=="select-one"){
//    alert(frm.elements[i].options[frm.elements[i].selectedIndex].value);
	
	var container = document.createElement("div"); 
	container.id = "div" + frm.elements[i].name;
	var hiddenTxt = document.createElement("input");
        hiddenTxt.setAttribute("name",frm.elements[i].name);
	hiddenTxt.type = "hidden";
	hiddenTxt.Name = frm.elements[i].name;
	hiddenTxt.value = frm.elements[i].options[frm.elements[i].selectedIndex].value;
	container.appendChild(hiddenTxt);
	if(document.getElementById('optionHidden'))
		document.getElementById('optionHidden').innerHTML += container.innerHTML;

   }
   }
}
function yesNo(){
var divOpt = document.getElementById('optionDiv').innerHTML;
var discount = document.getElementById('selectBid');
if(discount.options[discount.selectedIndex].index == 1){
var cnt = GetCookie("permission");
if(cnt){
	document.getElementById('divBid').innerHTML="<span id='alertText'>Ooops! You have tried maximum attempt of bidding...</span>"
	
if(document.getElementById('imgButton')){	document.getElementById('imgButton').style.visibility="hidden";
	document.getElementById('imgButton').style.display="none";
	
}
return false; 
}
else{
	if(!document.getElementById('imgButton')){
       
	document.getElementById('divBid').innerHTML='<span id="spanBid">You will get '+(3 - Number(count))+' attempts before you’re locked out for '+ expDays +' days...</span><br /> Make an Offer : <input class="input-txt-fill" id="bidprice" type="text"><br /><br /><select name="DesiredDiscount" style="visibility: hidden;"><option value="Yes">Yes</option></select><input type="hidden" id="aprice"><input id="imgButton" src="http://lib.store.yahoo.net/lib/yhst-17159114447480/makeanoffer.gif" type="image" style="visibility:visible;display:inline;">';
	
	}
	else {
        
         document.getElementById('divBid').innerHTML='<span id="spanBid">You will get '+(3 - Number(count))+' attempts before you’re locked out for '+ expDays +' days...</span><br />Make an Offer : <input class="input-txt-fill" id="bidprice" type="text"><br /><br /><select name="DesiredDiscount" style="visibility: hidden;"><option value="Yes">Yes</option></select><input type="hidden" id="aprice"><input id="imgButton" src="http://lib.store.yahoo.net/lib/yhst-17159114447480/makeanoffer.gif" type="image" style="visibility:visible;display:inline;">';
	
	}
return false;
}
}
}
function selection(){
var sel = document.getElementById('selectBid');
var btn = document.getElementById('imgButton');
 //btn.src = "http://lib.store.yahoo.net/lib/test-st51-indianic/addtocart-butt.jpg"
if(sel.options[sel.selectedIndex].index == 1){
				   
//btn.src = "http://lib.store.yahoo.net/lib/test-st51-indianic/bid-purchase-butt.gif"
flag = true;
				   document.getElementById('divBid').style.display = "inline";
				   document.getElementById('divBid').style.visibility = "visible";
//document.getElementById('imgButton').style.visibility="visible";
	//document.getElementById('imgButton').style.display="inline";
				   			   
				   }
else{
				   
//btn.src = "http://lib.store.yahoo.net/lib/test-st51-indianic/addtocart-butt.jpg";
                   flag = false;	           
				   document.getElementById('divBid').style.display = "none";
				   document.getElementById('divBid').style.visibility = "hidden";
//document.getElementById('imgButton').style.visibility="hidden";
	//document.getElementById('imgButton').style.display="none";
				   
				   

				   
}
}
var count = GetCookie("counterper");
if(!count)
{
count = 0;
}
var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
var expDays = 3;
function GetCookie(NameOfCookie)
{ if (document.cookie.length > 0)
{ begin = document.cookie.indexOf(NameOfCookie+"=");
if (begin != -1)
{ begin += NameOfCookie.length+1;
end = document.cookie.indexOf(";", begin);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(begin, end)); }
}
return null;
}


function SetCookie(name, value, expiredays)
{ 
var expiredays=expDays;
var ExpireDate = new Date ();
ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
document.cookie = name + "=" + escape(value) +
((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}
function bidding(obj){
var p = document.getElementById('baseprice').value;
var bidR = document.getElementById('bidRange').value;
var actual_price = p.substr(p.indexOf("$")+1, p.length);
var basep = parseFloat(actual_price);
var canbid = (basep * (100 - bidR)) / 100 ;
var bidpr = document.getElementById('bidprice').value;
bidpr = bidpr.substr(bidpr.indexOf("$")+1, bidpr.length);
var bidp = parseFloat(bidpr);

var getstr;
var sel = document.myform.DesiredDiscount;
//alert(sel);
//alert(canbid);
if (flag == true) {
	if (isNaN(bidp) == true){
		alert("Please Enter the proper bid");
		document.getElementById('bidprice').focus();
		return false;
	}
	else if (bidp < canbid || bidp > basep){var cnt = GetCookie("permission");
	if(count >= 2 || cnt){
		   document.getElementById('divBid').innerHTML="<span id='alertText'>Ooops! You have tried maximum attempt of bidding...</span>"
if(document.getElementById('imgButton')){
document.getElementById('imgButton').style.visibility="hidden";
document.getElementById('imgButton').style.display="none";
}
		SetCookie("permission","no");
return false;
	}else{

		//alert("Your bid is not qualified please try again...");
		document.getElementById('bidprice').focus();
                count = Number(count) + 1;
                SetCookie("counterper",count);
          document.getElementById('spanBid').innerHTML='<img src="http://lib.store.yahoo.net/lib/yhst-17159114447480/ajax-loader.gif" border="0" alt="Loading...">'
setTimeout("document.getElementById('spanBid').innerHTML='Your bid is not qualified. You have '+(3 - Number(count))+' more attempts remaining...'",5000);
		return false;
 
		}       
	}
	else {
	     /*for (i=0; i<obj.childNodes.length; i++) {
alert(obj.childNodes[i].tagName);
         				if (obj.childNodes[i].tagName == "input") {
							alert("hello input tag called");
		 					if(obj.childNodes[i].type == "image"){

            					var imgButton = obj.childNodes[i];
								alert(imgButton);
								imgButton.id = "TB_closeWindowButton";
							}
						}
					}*/
                
		document.getElementById('aprice').value = p;
		document.getElementById('aprice').name = "Actual Price ";

		basep = basep - bidp;

    
		//var sel = document.myform.Discount;
		sel.options[0].selected = true;
		if(sel.options[sel.selectedIndex].index == 0){
			getstr = sel.options[sel.selectedIndex].value;
				if(getstr == "Yes"){
            		sel.options[sel.selectedIndex].value=sel.options[sel.selectedIndex].value       + "(-" + "$" + toDollarsAndCents(basep) + ")";
						
				}
				else{
				sel.options[sel.selectedIndex].value = "Yes";
				sel.options[sel.selectedIndex].value=sel.options[sel.selectedIndex].value       + "(-" + "$" + toDollarsAndCents(basep) + ")";
				}
			//alert(getstr);
			}

		 document.getElementById('spanBid').innerHTML='<img src="http://lib.store.yahoo.net/lib/yhst-17159114447480/ajax-loader.gif" border="0">'
				setTimeout("document.getElementById('spanBid').innerHTML='You will get '+(3 - Number(count))+' attempts before you’re locked out for '+ expDays +' days...'",5000);	

	}

}
else
{
    //document.getElementById('divBid').innerHTML = ""; 
	//sel.options[sel.selectedIndex].index = 0;
	sel.options[sel.selectedIndex].value = "No";
	selection();
	
}
}
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
}
