function roundDecimal(iNumber)
{
	iNumber = '' + iNumber;
	iIndex = iNumber.indexOf('.');
	if(iIndex >= 0)
	{
		iEnd = eval(iIndex) + 2;
		iNumber = iNumber.substring(0,iEnd);
	}
	return iNumber;
}

function validateApplyDiscount(sUrl)
{
	var oForm = eval(document.getElementById('paypalform'));
	if (document.getElementById('paypalform').voucher.value == '')
	{
		alert('Please enter the voucher code');
		return false;
	}
	else
	{
// 		if(document.getElementById("onetime-discount").value != '1')
// 		{
		//document.getElementById("onetime-discount").value= '1';	
		document.getElementById("progressInfo").innerHTML = '<img src="http://www.seocompany.ca/admin/gui/img/spinner.gif" /> Verifying voucher code now...';
		
		var sPackages = oForm.packages.value;
		var sVoucherCode = oForm.voucher.value;
		var iPageCode = oForm.pageCode.value;
		var originalRequest = './app/sub/applyVoucher.php?voucher='+sVoucherCode+'&PageCode='+iPageCode;
		var xmlhttp = makeXmlHttpRequest();
        xmlhttp.open("GET", originalRequest);
        xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
        xmlhttp.send(null);
        xmlhttp.onreadystatechange = function()
        {
            if (xmlhttp.readyState == 4)
            {
                if (xmlhttp.status == 200)
                {
	                sResponse = xmlhttp.responseText;
	                sResponse1 = sResponse.substring(0,2);
	                
                    if (sResponse1 == '1-')
                    {	        
	                    document.getElementById('paypalform').add_stage.value = 1; 
	                    var iPercentEnd = sResponse.indexOf ("&");
	                    var iAmountEnd = sResponse.indexOf ("?");
	                    
	                    var iVoucherPercent = sResponse.substring(2,iPercentEnd);
	                    var iVoucherAmount = sResponse.substring(iPercentEnd + 1,iAmountEnd);
	                    var iVoucherLabour = sResponse.substring(iAmountEnd + 1);
	                    
                        var sNumList = document.getElementById('paypalform').num_links_list.value;
                    	var aNumList = sNumList.split(",");
                    	var iMax     = aNumList.length;
                    	document.getElementById("progressInfo").innerHTML = sNumList;
                    	iCurGroup = 0;
                    	var oVouPercent;
                        var oVouAmount;
                        var oVouLabour;
                    	for (var i = 0; i < iMax; i++)
                    	{
                    		var j = i+1;
                    		
                    		oVouPercent = eval('document.getElementById(\'paypalform\').hidVouPercent'+j);
 	    		            oVouAmount = eval('document.getElementById(\'paypalform\').hidVouAmount'+j);
 	    		            oVouLabour = eval('document.getElementById(\'paypalform\').hidVouLabour'+j);
 	    		            
 	    		            oVouPercent.value = iVoucherPercent;
 							oVouAmount.value = iVoucherAmount;
 							oVouLabour.value = iVoucherLabour;
                    		
                    		var sSpanId = 'pOff'+j;
                    		var oPOff = eval('document.getElementById(\''+sSpanId+'\')');
                    		oPOff.innerHTML = '<span class="red"></span> ';
                    		
                    		
                    		
                    		
                    		
                    		//generateImg(sVoucherPrice, oPOff);
                    		var oDiscountedPrice = eval('document.getElementById(\'paypalform\').hidDiscountedPrice'+j);

                    		id2 = 'hidDisplayType' + j;
							iDisplayType = eval(document.getElementById(id2)).value;
							id1= 'sPackageNo' + j;
							iCurPackageVal = eval(document.getElementById(id1)).value;
							if(iDisplayType == 1)
							{
								var sFormElement = eval('document.getElementById(\'paypalform\').product'+iCurPackageVal);
							}
							else
							{
								id3 = 'hidGroupId' + j;
								iGroupId = eval(document.getElementById(id3)).value;
								id3 = 'hidSize' + iGroupId;
								iSize = eval(document.getElementById(id3)).value;
								if(iSize == 1)
								{
									iIndex = 0;
									var sFormElement = eval('document.getElementById(\'paypalform\').products'+iGroupId);
								}
								else
								{
									if(iCurGroup == iGroupId)
									{
										iIndex++;
									}
									else
									{
										iCurGroup = iGroupId;
										iIndex = 0;
									}
									sFormElement = eval('document.getElementById(\'paypalform\').products'+iGroupId+'['+iIndex+']');
                				}
            				}
                    		//oDiscountedPrice.value = oDiscountedPrice.value - sVoucherPrice;
                    		oDiscountedPrice.value = roundDecimal(oDiscountedPrice.value);
                    		if(sFormElement.checked) 
                    		{
	                    		var sSecondArg = document.getElementById('paypalform').num_links.value+','+iCurPackageVal;
                				calcHourPrice(sFormElement,sSecondArg);
                    		}
                		}                    	
                		
                        //document.getElementById("voucherButton").innerHTML = "";
                        document.getElementById("progressInfo").innerHTML = "";  
                        retrieveVoucherValue(sVoucherCode);                      
                    }
                    else
                    {
                        document.getElementById("progressInfo").innerHTML = '<span class="red">Voucher Code is NOT valid.</span>';
                        document.getElementById('paypalform').add_stage.value = 7;
                        document.getElementById('totaloff').innerHTML = '';
                        
                        var sNumList = document.getElementById('paypalform').num_links_list.value;
	var aNumList = sNumList.split(",");
	var iMax     = aNumList.length;
	var theform = document.getElementById('paypalform');
	var newos1 = '';
	var m = 0;
	var bChecked = false;
	for(i = 0; i < iMax; i++)
	{
		j = i+1;
		
		var sSpanId = 'pOff'+j;
		var oPOff = eval('document.getElementById(\''+sSpanId+'\')');
		oPOff.innerHTML = '<span class="red"></span> ';
                    		
		id1= 'sPackageNo' + j;
		id = 'hidDisplayType' + j;
		iDisplayType = eval(document.getElementById(id)).value;
		if(iDisplayType == 1)
		{
			iCurPackageVal = eval(document.getElementById(id1)).value;
			sFormElement = eval('document.getElementById(\'paypalform\').product'+iCurPackageVal);
		}
		else
		{
			id3 = 'hidGroupId' + j;
			iGroupId = eval(document.getElementById(id3)).value;
			id3 = 'hidSize' + iGroupId;
			iSize = eval(document.getElementById(id3)).value;
			if(iSize == 1)
				var sFormElement = eval('document.getElementById(\'paypalform\').products'+iGroupId);
			else
			{
				bFound = false;
				for(iCt=0;iCt<iSize;iCt++)
				{
					if(!bFound)
					{
						sFormElement = eval('document.getElementById(\'paypalform\').products'+iGroupId+'['+iCt+']');
						if(sFormElement.checked)
						{
							bFound = true;
							i++;j++;
							break;
						}
					}
					i++;j++;
				}
				if(iCt == iSize)
				{ i--;j--; }
			}
		}
		if(sFormElement.checked)
		{
			var sSecondArg = document.getElementById('paypalform').num_links.value+','+iCurPackageVal;
            calcHourPrice(sFormElement,sSecondArg);
		}
	}
                        
                        
                        
                        
                    }
                }
                else
                {
                    alert("Failure!");
                    document.getElementById("progressInfo").innerHTML = "Server not responding. Please try later.";
                    
                }
            }
        };

		return true;
//	} //End of onetime-discount
	}
}
function retrieveVoucherValue(str)
{
		var originalRequest = './app/sub/retrieveVoucherValue.php?voucher='+str;
		var xmlhttp = makeXmlHttpRequest();
        xmlhttp.open("GET", originalRequest);
        xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
        xmlhttp.send(null);
        xmlhttp.onreadystatechange = function()
        {
            if (xmlhttp.readyState == 4)
            {
                if (xmlhttp.status == 200)
                {                    
	                    document.getElementById("voucher_val").innerHTML = xmlhttp.responseText;                   
                }
            }
        }                    
}

function isInteger(s)
{  
    var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9")) && (c != '-') && (c != '.')) 
        {
	        return false;
        }
    }
    // All characters are numbers.
   return true;
}

function validateMakePayment(sSalesFrontend)
{
   
	var sNumList = document.getElementById('paypalform').num_links_list.value;
	var aNumList = sNumList.split(",");
	var iMax     = aNumList.length;
	var theform = document.getElementById('paypalform');
	var newos1 = '';
	var m = 0;
	var bChecked = false;
	if(sSalesFrontend != 'blogdir')
	{
		for(i = 0; i < iMax; i++)
		{
			j = i+1;
			id1= 'sPackageNo' + j;
			id = 'hidDisplayType' + j;
			iDisplayType = eval(document.getElementById(id)).value;
			if(iDisplayType == 1)
			{
				iCurPackageVal = eval(document.getElementById(id1)).value;
				sFormElement = eval('document.getElementById(\'paypalform\').product'+iCurPackageVal);
			}
			else
			{
				id3 = 'hidGroupId' + j;
				iGroupId = eval(document.getElementById(id3)).value;
				id3 = 'hidSize' + iGroupId;
				iSize = eval(document.getElementById(id3)).value;
				if(iSize == 1)
					var sFormElement = eval('document.getElementById(\'paypalform\').products'+iGroupId);
				else
				{
					bFound = false;
					for(iCt=0;iCt<iSize;iCt++)
					{
						if(!bFound)
						{
							sFormElement = eval('document.getElementById(\'paypalform\').products'+iGroupId+'['+iCt+']');
							if(sFormElement.checked)
							{
								bFound = true;
								i++;j++;
								break;
							}
						}
						i++;j++;
					}
					if(iCt == iSize)
					{ i--;j--; }
				}
			}
			if(sFormElement.checked)
			{
				bChecked = true;
				break;
			}
		}
	}
	
	var sEmail     = document.getElementById('paypalform').email.value;
	var iMailLenth = sEmail.length;
	var bMail      = false;
    for(i = 0; i <= iMailLenth; i++)
    {
	    if(sEmail.charAt(i)=='@')
	    {
  	    	bMail = true;
     	}
    }
    
	if ((!bChecked) && (sSalesFrontend != 'blogdir'))
	{
		alert('You currently have not selected any products');
		return false;
	} 
	else if (document.getElementById('paypalform').fullname.value == '')
	{
		alert('Please enter Fullname');
		document.getElementById('paypalform').fullname.focus();
		return false;
	}
	else if (document.getElementById('paypalform').phone.value == '')
	{
		alert('Please enter Phone');
		document.getElementById('paypalform').phone.focus();
		return false;
	}
	else if (!(isInteger(document.getElementById('paypalform').phone.value))) 
	{
		alert('The field phone number can contain only numbers.');
		document.getElementById('paypalform').phone.focus();
		return false;		
	} 
	else if (document.getElementById('paypalform').url.value == '')
	{
		alert('Please enter URL');
		document.getElementById('paypalform').url.focus();
		return false;
	}
	 else if ( (document.getElementById('paypalform').url.value != '') && document.getElementById('paypalform')['url'].value.substring(0,7) != 'http://') 
	 {
		alert('URL requires the http:// prefix e.g. http://www.domain.com or http://subdomain.domain.com');
		document.getElementById('paypalform').url.focus();
		return false;
		
	} 
	
	else if (document.getElementById('paypalform').email.value == '')
	{
		alert('Please enter Email');
		document.getElementById('paypalform').email.focus();
		return false;
	}
	else if(!bMail)
    {	   
   		alert("The email address field value is not in proper format.");
   		document.getElementById('paypalform').email.focus();
   		return false;
    }
    else if(document.getElementById("txtSecurityCode") && document.getElementById("txtSecurityCode").value == '')
    {
	    alert("Please enter Security Code");
   		document.getElementById("txtSecurityCode").focus();
   		return false;
	}
	else if(document.getElementById("txtSecurityCode") &&  document.getElementById("txtSecurityCode").value != '')
    {
	         
			var url = './app/sub/validate-captcha.php';
			//Set up the parameters of our AJAX call
			var postStr = document.getElementById("txtSecurityCode").name + "=" + encodeURIComponent( document.getElementById("txtSecurityCode").value );
			//Call the function that initiate the AJAX request
			makeRequest(url, postStr);
			

	}
	else
	{
		document.getElementById('paypalform').add_stage.value = 2;
		document.getElementById('paypalform').submit();
		return true;
	}
}
//Gets the browser specific XmlHttpRequest Object 
function getXmlHttpRequestObject() {
 if (window.XMLHttpRequest) {
    return new XMLHttpRequest(); //Mozilla, Safari ...
 } else if (window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP"); //IE
 } else {
    //Display our error message
    alert("Your browser doesn't support the XmlHttpRequest object.");
 }
}

//Our XmlHttpRequest object
var receiveReq = getXmlHttpRequestObject();



//Initiate the AJAX request
function makeRequest(url, param) {
//If our readystate is either not started or finished, initiate a new request
 if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
   //Set up the connection to captcha_test.html. True sets the request to asyncronous(default) 
   receiveReq.open("POST", url, true);
   //Set the function that will be called when the XmlHttpRequest objects state changes
   receiveReq.onreadystatechange = updatePage; 

   //Add HTTP headers to the request
   receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   receiveReq.setRequestHeader("Content-length", param.length);
   receiveReq.setRequestHeader("Connection", "close");

   //Make the request
   receiveReq.send(param);
 }   
}

//Called every time our XmlHttpRequest objects state changes
function updatePage() {
 //Check if our response is ready
 if (receiveReq.readyState == 4) {
	 
	 
	 if(receiveReq.responseText == 'Invalid security code')
	 {
		 //Set the content of the DIV element with the response text
		   document.getElementById('security_code').innerHTML = receiveReq.responseText;
		   //Get a reference to CAPTCHA image
		   img = document.getElementById('imgCaptcha'); 
		   //Change the image
		   img.src = './app/sub/imageblob.php?' + Math.random();
	 }
	 else if(receiveReq.responseText == '<h1>Test successful!</h1>')
	 {
		 //Set the content of the DIV element with the response text
		 //document.getElementById('security_code').innerHTML = receiveReq.responseText;
		 document.getElementById('paypalform').add_stage.value = 2;
		 document.getElementById('paypalform').submit();
		 return true;
	 }
   
 }
 
}

function makeXmlHttpRequest()
{
    var xmlhttp = false;
    if (window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest()
    }
    else if (window.ActiveXObject)
    {
        try
        {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(E)
            {
                window.location.reload('uploadFile.aspx');
            }
        }
    }
    return xmlhttp;
}
