var ns6=document.getElementById&&!document.all

function restrictinput(maxlength,e,placeholder)
{
        if (window.event&&event.srcElement.value.length>=maxlength)
        return false
        else if (e.target&&e.target==eval(placeholder)&&e.target.value.length>=maxlength)
        {
                var pressedkey=/[a-zA-Z0-9\.\,\/]/ //detect alphanumeric keys
                if (pressedkey.test(String.fromCharCode(e.which)))
                e.stopPropagation()
        }
}

function countlimit(maxlength,e,placeholder)
{
        var theform=eval(placeholder)
        var lengthleft=maxlength-theform.value.length
        var placeholderobj=document.all? document.all[placeholder] :                                                          document.getElementById(placeholder)
        if (window.event||e.target&&e.target==eval(placeholder))
        {
                if (lengthleft<0)
                        theform.value=theform.value.substring(0,maxlength)
                        placeholderobj.innerHTML=lengthleft
        }
}

function displaylimit(theform,thelimit)
{
        var limit_text='<b><span id="'+theform.toString()+'">'+thelimit+'</span></b> characters remaining on your input limit'

        if (document.all||ns6)
                document.write(limit_text)
        if (document.all)
        {
                eval(theform).onkeypress=function(){ return restrictinput(thelimit,event,theform)}
                eval(theform).onkeyup=function(){ countlimit(thelimit,event,theform)}
        }
        else if (ns6)
        {
                document.body.addEventListener('keypress', function(event)
                 { restrictinput(thelimit,event,theform) }, true);
                        document.body.addEventListener('keyup', function(event)
                         { countlimit(thelimit,event,theform) }, true);
        }
}


function openNewWin(strUrl, strHeigh, strWidth)
{
        self.open(strUrl,"","toolbar=0,status=0,menubar=0,top=50,left=100,height=" + strHeigh + ",width=" + strWidth + ",resizable=0,scrollbars=yes");
}


function splitURL(strUrl)
{
         var preFix = strUrl.substr(0,4);
         var preFixFinal = preFix.toLowerCase();

         if (preFixFinal == "http")
         {
             return true;
         }
         else
         {
             return false;
         }
}


function UnApprove(id, strType)
{
       window.open("UnApprove.php?id=" + id + "&type=" + strType + "","unapprove","toolbar=0,status=0,menubar=0,top=0,left=0,height=5,width=5,resizable=0,scrollbars=no");

}


function chkSelect()
{

    var oldPrefix;
    var privousprice;
    var n = document.baysubmit.bcat.length;
    var j = 1;

    for(i=0; i<n; i++)
    {
        if(document.baysubmit.bcat[i].selected == true)
        {
        	
            var prefix = document.baysubmit.bcat[i].value.split(':')[0];
            /*
            if ("13" == prefix)
            {
            	document.all.realopt.className = "dsp_table";
            }
            else
            {
            	document.all.realopt.className = "undsp_table";
            }*/
            
            var adprice = Price_List(parseInt(prefix), 1);
            
            //if (j != 1 && oldPrefix != prefix || (oldPrefix == prefix && prefix == 13))
            //if (j != 1 && oldPrefix != prefix)
            if (j != 1 && privousprice != adprice)
            {
                alert('Photo Ads cannot be assigned to two differently-Priced categories; please select the most appropriate category for your ad.');

								clean_Selected(n);
                return false;
            }

            //oldPrefix = prefix;
            privousprice = adprice;
            j++;
        }
    }
    
    chkMaxSelect();
}

function clean_Selected(rowcount)
{
    for(i=0; i<rowcount; i++)
    {
        if(document.baysubmit.bcat[i].selected == true)
        {
            document.baysubmit.bcat[i].selected = false;
        }
    }	
}

function chkMaxSelect()
{
    var n = document.baysubmit.bcat.length;
    var maxItem = 4;
    var j = 0;

    for(i=0; i<n; i++)
    {
        if(document.baysubmit.bcat[i].selected == true)
        {
        	j++;	
        }
        
        if (j > maxItem)
        {
        	alert("Please select a maximum of four categories.");
        	clean_Selected(n);
        	return false;
        }
    }
}    

//for ad modify to check if the UP Front ad and only can select single category & mehtod
function Chk_MultipleCateMethod4AdModify()
{

    var oldPrefix;
    var nLenCate = document.baysubmit.bcat.length;
    var nLenMth = document.baysubmit.mth.length;
    var bValidUpFront = document.all.upfront.value;
    var horseSaleandLeaseOut = "";
    var i = 0;
    var j = 0;

    if ("TRUE" == bValidUpFront)
    {
        for(m=0; m<nLenCate; m++)
        {
            if (document.baysubmit.bcat[m].selected)
            {
                    i++;
            }
        }

        for(n=0; n<nLenMth; n++)
        {
            if (document.baysubmit.mth[n].checked)
            {                             
                    mthval = document.baysubmit.mth[n].value;
                    horseSaleandLeaseOut += horseSaleandLeaseOut==""? mthval : "," + mthval;
                    j++;
            }
        }
        
        if (1 < i || (1 < j && "1,3" != horseSaleandLeaseOut))
        {
            alert("This is an UpFront ad, please select only one breed/category!");
            return false;
        }
        else
        {
            return true;
        }
    }
    else
    {
        return true;
    }

}

function Busi_ChkCateSelect()
{

    var oldPrefix;
    var n = document.baysubmit.bcat.length;
    var j = 0;

    document.baysubmit.bcat[0].selected = false;

    for(i=1; i<n; i++)
    {
        if (document.baysubmit.bcat[i].selected)
        {
                j++;
        }
    }

    if (0 == j)
    {
        return false;
    }
    else
    {
        return true;
    }

}

function smtForm(form, vid)
{
         //var nTotalPrice = document.all.txtPriceDis.value;
         var nTotalPrice = document.all.Price.value;
         
         if (isNaN(nTotalPrice) || "0.00" == nTotalPrice || "" == nTotalPrice || "0" == nTotalPrice || ".00" == nTotalPrice)
         {
              alert("The total price can not be Zero");
              return false;
         }
         else
         {
         				 ////////////////////////////////
         				 // Here to update the record's expiration date
         				 ///////////////////////////////
				         var nRecID = document.all.nRecID.value;
				         var strAdType = document.all.AdType.value;
				         var strMainType = document.all.MainType.value;
				         var strIfShoppingcart = document.all.Scart.value;
				         var nAdPeriod;
				         var nUFperiod;
				         
				         if ("Photo" == strAdType && "shoppingcart" != strIfShoppingcart)
				         {
				         		nAdPeriod = Chk_AdMth();
				         		nUFperiod = Chk_HeadMth();
				         }	

				         //////////////////////
				         // End update expiration date
				         /////////////////////
				         
				           	
                 if  ("1" == vid)
                 {
                         //myform.action = "https://www.bayequest.info/ssl/linkpointprocess.php";
                         form.action = "https://www.bayequest.info/ssl/linkpointprocess.php";
                 }
                 else
                 {
                         //form.action = "https://www.paypal.com/cgi-bin/webscr";
                         form.action = "/paypal/GotoPaypal.php";
                 }

                 form.submit();
         }
}


function TotalPrice(pid, currOriginalPrice)
{

        pid = parseFloat(pid);
        //pid = pid.toFixed(2);

        currOriginalPrice = parseFloat(currOriginalPrice);
        currOriginalPrice = currOriginalPrice.toFixed(2);

        document.all.txtPriceDis.value = pid.toFixed(2);;
        document.all.txtPrice.value = currOriginalPrice;
        document.all.amount.value = pid;
        document.all.Price.value = pid;

}

/*
function putvalue(id)
{
         var tid;
         if (document.all.cbxHead.checked)
         {
             tid = parseFloat(id) + 5;
         }
         else
         {
             tid = id;
         }

         TotalPrice(tid);
}
*/

//for adsfinish.php
function Chk_AdMth()
{
    var admth;
    var nLen = document.all.admth.length;

    for (var i=0; i<nLen; i++)
    {
        if (document.all.admth[i].checked)
        {
            admth = document.all.admth[i].value;
        }
    }
    return admth;
}

//for adsfinish.php
function Chk_HeadMth()
{
    var upmth;
    var nLen = document.all.upmth.length;

    for (var i=0; i<nLen; i++)
    {
        if (document.all.upmth[i].checked)
        {
            upmth = document.all.upmth[i].value;
        }
    }
    return upmth;
}

//for shopping cart
function Chk_AdMth2(id)
{
    var admth;
    var nLen = document.all["admth"+id].length;

    for (var i=0; i<nLen; i++)
    {
        if (document.all["admth"+id][i].checked)
        {
            admth = document.all["admth"+id][i].value;
        }
    }
    return admth;
}

//for shopping cart
function Chk_HeadMth2(id)
{
    var upmth;
    var nLen = document.all["upmth"+id].length;

    for (var i=0; i<nLen; i++)
    {
        if (document.all["upmth"+id][i].checked)
        {
            upmth = document.all["upmth"+id][i].value;
        }
    }
    return upmth;
}

//For shopping card ref.
function chk_mth(id)
{
    var admth = parseInt(Chk_AdMth2(id));
    var headmth = parseInt(Chk_HeadMth2(id));
    var nLocate;

    if (headmth > admth)
    {
        alert('Up-Front Ad can\'t display longer than regular ad period.');
        self.location.reload();
        return;
    }
    else
    {
        //check multiple select
        if (document.all["txtMultiCate" + id].value == "Yes" && 0 != Chk_HeadMth2(id))
        {

        				var ys = confirm("UpFront spotlight status can be applied to ads submitted under one\nbreed/ad category only. You have selected more than one breed/ad\ncategory for this ad. Please return to your user control panel to modify\nyour ad,or contact us for assistance at info@bayequest.com.");                  
                if (ys)
                {
                    var cid = document.all["clasiid"+id].value;
                    var sta = document.all["status"+id].value;
                    var type = document.all["type"+id].value;

                    self.open("/b001/adsmodify.php?adsid="+cid+"&status="+sta+"&type="+type,"op","");
                }
                else
                {
                    document.all["upmth"+id][3].checked = true;
                    return false;
                }
        }
        else
        {
            var strMth = Chk_AdMth2(id);
            var strHead = Chk_HeadMth2(id);
            var nAdID = document.all["myID"+id].value;
            var strUrl = "shoppingcart.php?Type=update&myID=" + nAdID + "&Head=" + headmth + "&Mth=" + admth + "";
            location = strUrl;

        }
    }

    self.opener.location.reload();
}


function Chk_UpFrontLocation(upfrontid)
{
    var nRetLocate;

    switch (upfrontid)
    {
        case 0:
            nRetLocate = 3;
            break;

        case 2:
            nRetLocate = 0;
            break;

        case 6:
            nRetLocate = 1;
            break;

        case 12:
            nRetLocate = 2;
            break;
    }

    return nRetLocate;
}


//Up-front ad checked
function smtHead(nMainTypeID, mth, bLevel)
{
    var currPricing;
    var currUpFrontPricing;
    var currPricingBeforeDiscount;
    var mAdDisMth = Chk_AdMth();

    if (document.all.txtMultiCate.value == "Yes")
    {
        				var ys = confirm("UpFront spotlight status can be applied to ads submitted under one\nbreed/ad category only. You have selected more than one breed/ad\ncategory for this ad. Please return to your user control panel to modify\nyour ad,or contact us for assistance at info@bayequest.com."); 
        if (ys)
        {

            var adtype = document.all.AdType.value;
            var maintype = document.all.MainType.value;

            if ("horse" == maintype)
            {
                window.location.href = "horse_ads_in1.php?ctype=" + adtype + "";
            }
            else if ("other" == maintype)
            {
                window.location.href = "other_ads_in1.php?ctype=" + adtype + "";
            }

            return;
        }
        else
        {
            document.all.upmth[3].checked = true;
            return;
        }
    }

    if (parseInt(mth) > parseInt(mAdDisMth))
    {
        alert('Up-Front Ad can\'t display longer than regular ad period.');
        document.all.upmth[3].checked = true;
        return;
    }

      if ("1" == nMainTypeID || "8" == nMainTypeID || "12" == nMainTypeID || "13" == nMainTypeID)
      {
          currAdditional = 150;
      }
      else if ("7" == nMainTypeID || "9" == nMainTypeID || "10" == nMainTypeID|| "11" == nMainTypeID)
      {
          currAdditional = 100;
      }

      currPricing = parseFloat(document.all.txtPriceDis.value);
      currPricingBeforeDiscount = parseFloat(document.all.txtPrice.value);

      if (document.all.txtUpPriceDis.value != "")
      {
            currPricing -= parseFloat(document.all.txtUpPriceDis.value);
      }

      if (document.all.txtUpPrice.value != "")
      {
            currPricingBeforeDiscount -= parseFloat(document.all.txtUpPrice.value);
      }

      currAdditional *= (mth/2);
      currAdditionalBeforeDiscount = currAdditional;

      if (bLevel == "True")
      {
          currAdditional *= 0.75;
      }

       if (6 == mth)
       {
             currAdditional *= 0.9;
       }
       else if (12 == mth)
       {
             currAdditional *= 0.85;
       }

      currUpFrontPricing = currPricing + currAdditional;
      currUpFrontPricingBeforeDiscount = currPricingBeforeDiscount + currAdditionalBeforeDiscount;

      document.all.txtUpPriceDis.value = currAdditional;
      document.all.txtUpPrice.value = currAdditionalBeforeDiscount;

      TotalPrice(currUpFrontPricing, currUpFrontPricingBeforeDiscount);
}



function Price_List(maincateid, adsnum)
{
		   var getprice;

		   //Main Category, 1-Horse for sale/Lease/Stallion, 8-Trailers & Tow Vehicles, 12-Employment
       switch (maincateid)
       {

        case 1:
              if (1 == adsnum){ getprice = 22; }
              else if(2 == adsnum){ getprice = 30; }
              else if(3 == adsnum){ getprice = 38; }
              break;

        case 8:
              if (1 == adsnum){ getprice = 22; }
              else if(2 == adsnum){ getprice = 30; }
              else if(3 == adsnum){ getprice = 38; }
              break;

        case 12:
              if (1 == adsnum){ getprice = 22; }
              else if(2 == adsnum){ getprice = 30; }
              else if(3 == adsnum){ getprice = 38; }
              break;

        case 13:
              if (1 == adsnum){ getprice = 22; }
              else if(2 == adsnum){ getprice = 30; }
              else if(3 == adsnum){ getprice = 38; }
              break;


        case 7:
              if (1 == adsnum){ getprice = 6; }
              else if(2 == adsnum){ getprice = 10; }
              else if(3 == adsnum){ getprice = 12; }
              break;

        case 9:
              if (1 == adsnum){ getprice = 6; }
              else if(2 == adsnum){ getprice = 10; }
              else if(3 == adsnum){ getprice = 12; }
              break;

        case 10:
              if (1 == adsnum){ getprice = 6; }
              else if(2 == adsnum){ getprice = 10; }
              else if(3 == adsnum){ getprice = 12; }
              break;

        case 11:
              if (1 == adsnum){ getprice = 6; }
              else if(2 == adsnum){ getprice = 10; }
              else if(3 == adsnum){ getprice = 12; }
              break;
              
        case 16:
              if (1 == adsnum){ getprice = 22; }
              else if(2 == adsnum){ getprice = 30; }
              else if(3 == adsnum){ getprice = 38; }
              break;              

       }
       
       return getprice;	
}


//How many months to display
function ChkPrice(ifmember, adsnum, MainID, monthNum)
{

       var totalPrice;
       var dUpFrontMth = Chk_HeadMth();

			 totalPrice = Price_List(MainID, adsnum);

       totalPrice *= (monthNum/2);
       totalPriceBeforeDiscount = totalPrice;

       if (ifmember == "T")
       {
        totalPrice *= 0.75;
       }

       if (6 == monthNum)
       {
             totalPrice *= 0.9;
       }
       else if (12 == monthNum)
       {
             totalPrice *= 0.85;
       }

         var tid;
         if (document.all.txtUpPrice.value != "" || document.all.txtUpPriceDis.value != "")
         {

             if (document.all.txtUpPrice.value != "")
             {
                 tid = parseFloat(totalPrice) + parseFloat(document.all.txtUpPriceDis.value);
             }

             if (document.all.txtUpPriceDis.value != "")
             {
                 tidBeforeDiscount = parseFloat(totalPriceBeforeDiscount) + parseFloat(document.all.txtUpPrice.value);
             }

         }
         else
         {
             tid = totalPrice;
             tidBeforeDiscount = totalPriceBeforeDiscount;
         }

        var oldPrice = document.all.oldPrice.value;

        if ("" != oldPrice)
        {
                oldPrice = parseFloat(oldPrice);
                tid -= oldPrice;
                tidBeforeDiscount -= oldPrice;
        }

			//video fee
			//tid += parseInt(document.all.videoPrice.value); 
			//tidBeforeDiscount += parseInt(document.all.videoPriceOri.value); 
			
      TotalPrice(tid, tidBeforeDiscount);

}


//pay member
function Pay_Member(nprice)
{
    TotalPrice(nprice, nprice);
    //document.all.Price.value = nprice;
}

function Disabled_UpFront()
{
    document.all.upmth[0].disabled = true;
    document.all.upmth[1].disabled = true;
    document.all.upmth[2].disabled = true;
    document.all.upmth[3].disabled = true;
}

function Disabled_UpFrontForCart(nId)
{
    document.all["upmth" + nId][0].disabled = true;
    document.all["upmth" + nId][1].disabled = true;
    document.all["upmth" + nId][2].disabled = true;
    document.all["upmth" + nId][3].disabled = true;
}

function Set_Premiumpage(bStatus, bCBX)
{
	var strPremiumpageContent;
	
	if (bStatus)
	{
		if (bCBX)
		{
				strPremiumpageContent = "<table id='tbl_premium' border=0 bgcolor='#E4F3CF' cellpadding=0 cellspacing=0 width='419'><tr><td><input type='checkbox' checked name='cbxpremium'>Do you want this ad for a Premium ad?</td></tr></table>";
	  }
	  else
	  {
	  		strPremiumpageContent = "<table id='tbl_premium' border=0 bgcolor='#E4F3CF' cellpadding=0 cellspacing=0 width='419'><tr><td><input type='checkbox' name='cbxpremium'>Do you want this ad for a Premium ad?</td></tr></table>";
	  }
	}
	else
	{
		strPremiumpageContent = "";
	}
	
	document.all.PremiumTag.innerHTML = strPremiumpageContent;
}

function Set_Video(bStatus)
{
	//bNeworModify, 0 - New, 1-Modify
	var strVideoContent;
	
	if (bStatus)
	{
	  //strVideoContent = "<table id='tbl_premium' border=0 bgcolor='#E4F3CF' cellpadding=0 cellspacing=0 width='419'><tr><td><input type='checkbox' name='cbxpremium'>Do you want this ad for a Premium ad?</td></tr></table>";
	  strVideoContent = "<table width=100%><tr><td><object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' width='300' height='30'><param name='movie' value='../v001/BAENvideoup.swf' /><param name='quality' value='high' /><embed src='../v001/BAENvideoup.swf' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='300' height='30'></embed></object></td><td> Play Time: <select name='videopt'><option value='1'>Within 1 min.</option><option value='3'>Within 3 min.</option><option value='5'>Within 5 min.</option></select></td></tr></table>";
	}
	else
	{
		strVideoContent = "";
	}
	
	document.all.VideoTag.innerHTML = strVideoContent;
}


function Chk_AskPriceMorethan25K()
{
		var cAskpricing = parseFloat(document.all.asking_price.value);
		
		if (cAskpricing > 25000)
		{
			return true;
		}
		else
		{
			return false;
		}
}

function setVideoName(strVname)
{
	
	document.baysubmit.hidVname2.value = strVname.split(":")[0];
	//alert(document.baysubmit.hidVname2.value);
	
}

function alertVideo()
{
	alert("The video file size too large. Must less than 100M");
}

function setVideoPrice(MainID, tMin, monthNum, ifmember)
{
		retPrice = "";
	
	  switch (MainID)
	  {
	
	      case 1:
	            if (1 == tMin){ retPrice = 49; }
	            else if(2 == tMin){ retPrice = 79; }
	            //else if(5 == tMin){ retPrice = 50; }
	            break; 	                           
	  }	 
	  
       retPrice *= (monthNum/2);
       retPriceBeforeDiscount = retPrice;	  
  
       if (ifmember == "T")
       {
        	retPrice *= 0.75;
       }

       if (6 == monthNum)
       {
             retPrice *= 0.9;
       }
       else if (12 == monthNum)
       {
             retPrice *= 0.85;
       }
       //alert(retPrice);

         var tid;
         if (document.all.txtUpPrice.value != "" || document.all.txtUpPriceDis.value != "")
         {

             if (document.all.txtUpPrice.value != "")
             {
                 tid = parseFloat(retPrice) + parseFloat(document.all.txtUpPriceDis.value);
             }

             if (document.all.txtUpPriceDis.value != "")
             {
                 tidPriceBeforeDiscount = parseFloat(retPriceBeforeDiscount) + parseFloat(document.all.txtUpPrice.value);
             }

         }
         else
         {
             tid = retPrice;
             tidPriceBeforeDiscount = retPriceBeforeDiscount;
         }
         
         //alert(tid);
         //alert(tidPriceBeforeDiscount);

        var oldPrice = document.all.oldPrice.value;

        if ("" != oldPrice)
        {
                oldPrice = parseFloat(oldPrice);
                tid -= oldPrice;
                tidPriceBeforeDiscount -= oldPrice;
        }

      TotalPrice(tid, tidPriceBeforeDiscount);
} 

//for payment info input form
function setPayInfo(ctype, cardnum, expmth, expyr, bname, straddr, city, state, zip, email, ccode)
{
		document.all.cardnumber.value = cardnum;
		document.all.bname.value = bname;
		document.all.addr.value = straddr;
		document.all.bcity.value = city;
		document.all.bstate.value = state;
		document.all.bzip.value = zip;
		document.all.email.value = email;
		document.all.cvm.value = ccode;
		
		switch (ctype)
		{
			case "V":		
				document.all.cctype[1].selected = true;
				break;
				
			case "M":
				document.all.cctype[2].selected = true;
				break;
				
			case "A":
				document.all.cctype[3].selected = true;
				break;
				
			case "D":
				document.all.cctype[4].selected = true;
				break;
		}
		
		for (i = 0; i < document.all.expmonth.length; i++)
		{
			//alert(expmth);
			//alert(document.all.expmonth[i].value);
			if (document.all.expmonth[i].value == expmth)
			{
				document.all.expmonth[i].selected = true;
			}
		}
		
		for (j = 0; j < document.all.expyear.length; j++)
		{
			if (document.all.expyear[j].value == expyr)
			{
				document.all.expyear[j].selected = true;
			}
		}
		
}








