function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         	IsNumber = false;
         }
      }
   return IsNumber;
   
}

function notNull(value , divId ,message)
{      
        if(value.length == 0)
        {
            document.getElementById(divId).innerHTML = "<font color='#FF0000' size='4'>" + message + "</font><br>";
            return false;
        }
        else
        {
            document.getElementById(divId).innerHTML = "" ;
            return true;
        }
}

function isItRightLength(value , length)
{
	if(value.length == parseInt(length))
	{
		return true;
	}
	else
    {
          return false;
	}
}

var cardNumError = ' ' ;
var pinError = '';
     
     
function submitThisForm()
{
         formIsOk = true;
         
         if(!(isItRightLength(document.forms[0].cardNumber.value , "16") && IsNumeric(document.forms[0].cardNumber.value)))
         {
            document.getElementById("cardErrors").innerHTML = "<font color='#FF0000'>" + cardNumError + "</font><br>";
            formIsOk = false;
         }

           if(!(isItRightLength(document.forms[0].pin.value , "4") && IsNumeric(document.forms[0].pin.value)))
            {
                document.getElementById("pinErrors").innerHTML = "<font color='#FF0000'>" + pinError + "</font><br>";
                formIsOk = false;
            }
         
          
         if(formIsOk)
         {
            document.forms[0].submit();
         }
}

function notLessThan(value , length)
{
	if(value.length >= parseInt(length))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function notMoreThan(value , length)
{
	if(value.length <= parseInt(length))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function isAlphaNumeric(val)
{
	if (val.match(/^[a-zA-Z0-9]+$/))
	{
		return true;
	}
	else
	{
		return false;
	}
} 

//Email Validation
function isEmailAddr(email)
{
  var result = false
  var theStr = new String(email)
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function RT_stripspaces(obj, m_which)
{
	var str, res;
	str = obj.value;
        len = str.length;
	res = "";
	switch (m_which)
	{ 
		case 1: // strip all spaces from this field
		{
                        trim = false;
			for (i=0; i<str.length; i++)
				if (str.charAt(i) == " "){
				  trim = true;
                                  break; 
                                }
                              if ( trim ){
                                alert("Input value containing spaces are not allowed");	
			        obj.value = "";
                                obj.focus(); 
                                return false;
                               }
                             
			break;
		}
		case 2: // trim spaces from begining and end of this field
		{
			var pos1, pos2;
			pos1 = pos2 = 0;
			for (i=0; i<str.length; i++)
				if (str.charAt(i) != " ")
				{
					pos1 = i;
					break;
				}
			for (i=str.length-1; i>=pos1; i--)
				if (str.charAt(i) != " ")
				{
					pos2 = i;
					break;
				}
			obj.value = str.substr(pos1, pos2 - pos1 + 1);

                        if ( obj.value.length < len ){
                            obj.select();
                            alert("Spaces in the input value are trimed");
                          } 
                        break;
		}
                case 3: // strip all spaces from this field without notification
		{
			for (i=0; i<str.length; i++)
				if (str.charAt(i) != " ")
					res = res + str.charAt(i);
			obj.value = res;
			break;
		}
	}
  
 return true;
}
//Validate Advertiser Registration 

function validateAdvertiserSignupForm(form)
{
    var valid = true;
    document.getElementById("nameerror").innerHTML = "<font color='#FF0000'>&nbsp;</font><br>";
    document.getElementById("passworderror").innerHTML = "<font color='#FF0000'>&nbsp;</font><br>";
	document.getElementById("password2error").innerHTML = "<font color='#FF0000'>&nbsp;</font><br>";
	document.getElementById("contacterror").innerHTML = "<font color='#FF0000'>&nbsp;</font><br>";
	document.getElementById("emailerror").innerHTML = "<font color='#FF0000'>&nbsp;</font><br>";
	document.getElementById("countryerror").innerHTML = "<font color='#FF0000'>&nbsp;</font><br>";

//	RT_stripspaces(form.Name , 2);
	if(!(notLessThan(form.Name.value , 1)))
    {
        document.getElementById("nameerror").innerHTML = "<font color='#FF0000'><strong>Company Name is required</strong></font><br>";
        valid = false
    }
//	else if(!(isAlphaNumeric(form.Name.value)))
  //  {
    //     errorMessages = errorMessages + "<br>" + usernameAlphanumeric;
      //   valid = false;
   // }
//    if(!(notMoreThan(form.Name.value , 10)))
//    {
//         errorMessages = errorMessages + "<br>" + usernameNotMoreThan;
//         valid = false;
//    }

    if(!(notLessThan(form.Password.value , 1)))
    {
        document.getElementById("passworderror").innerHTML = "<font color='#FF0000'><strong>Password is required</strong></font><br>";
         valid = false;
    }
    else if(!(notLessThan(form.Password.value , 6)))
    {
        document.getElementById("passworderror").innerHTML = "<font color='#FF0000'><strong>Password not less than 6 characters</strong></font><br>";
         valid = false;
    }
    else if(!(notMoreThan(form.Password.value , 10)))
    {
        document.getElementById("passworderror").innerHTML = "<font color='#FF0000'><strong>Password not more than 10 characters</strong></font><br>";
         valid = false;
    }
    else if(form.Password.value != form.Password2.value)
    {
        document.getElementById("password2error").innerHTML = "<font color='#FF0000'><strong>Password does not match</strong></font><br>";
         valid = false;
    }

    if(!(notLessThan(form.contact.value , 1)))
    {
        document.getElementById("contacterror").innerHTML = "<font color='#FF0000'><strong>Contact Name is required</strong></font><br>";
        valid = false;
    }
	else if(!(notMoreThan(form.contact.value , 30)))
    {
        document.getElementById("contacterror").innerHTML = "<font color='#FF0000'><strong>Contact Name not more than 30 characters</strong></font><br>";
        valid = false;
    }

    if(form.Email.value.length < 1)
    {
        document.getElementById("emailerror").innerHTML = "<font color='#FF0000'><strong>Email is required</strong></font><br>";
         valid = false;
    }
    else if(!(isEmailAddr(form.Email.value)))
    {
        document.getElementById("emailerror").innerHTML = "<font color='#FF0000'><strong>Email is not valid</strong></font><br>";
         valid = false;
    }

	if(form.Country.options[0].selected)
    {
        document.getElementById("countryerror").innerHTML = "<font color='#FF0000'><strong>Country is required</strong></font><br>";
         valid = false;
    }

    if(valid)
    {
		return true;
    }
    else
    {
		return false;
    }
}

//Validate Publisher Registration 
function validatePublisherSignupForm(form)
{
    var valid = true;
    document.getElementById("nameerror").innerHTML = "<font color='#FF0000'>&nbsp;</font><br>";
    document.getElementById("passworderror").innerHTML = "<font color='#FF0000'>&nbsp;</font><br>";
	document.getElementById("password2error").innerHTML = "<font color='#FF0000'>&nbsp;</font><br>";
	document.getElementById("contacterror").innerHTML = "<font color='#FF0000'>&nbsp;</font><br>";
	document.getElementById("emailerror").innerHTML = "<font color='#FF0000'>&nbsp;</font><br>";
	document.getElementById("webinfoerror").innerHTML = "<font color='#FF0000'>&nbsp;</font><br>";
	document.getElementById("weburlerror").innerHTML = "<font color='#FF0000'>&nbsp;</font><br>";
	document.getElementById("countryerror").innerHTML = "<font color='#FF0000'>&nbsp;</font><br>";

//	RT_stripspaces(form.Name , 2);
	if(!(notLessThan(form.Name.value , 1)))
    {
        document.getElementById("nameerror").innerHTML = "<font color='#FF0000'><strong>Company Name is required</strong></font><br>";
        valid = false
    }
//	else if(!(isAlphaNumeric(form.Name.value)))
  //  {
    //     errorMessages = errorMessages + "<br>" + usernameAlphanumeric;
      //   valid = false;
   // }
//    if(!(notMoreThan(form.Name.value , 10)))
//    {
//         errorMessages = errorMessages + "<br>" + usernameNotMoreThan;
//         valid = false;
//    }

    if(!(notLessThan(form.Password.value , 1)))
    {
        document.getElementById("passworderror").innerHTML = "<font color='#FF0000'><strong>Password is required</strong></font><br>";
         valid = false;
    }
    else if(!(notLessThan(form.Password.value , 6)))
    {
        document.getElementById("passworderror").innerHTML = "<font color='#FF0000'><strong>Password not less than 6 characters</strong></font><br>";
         valid = false;
    }
    else if(!(notMoreThan(form.Password.value , 10)))
    {
        document.getElementById("passworderror").innerHTML = "<font color='#FF0000'><strong>Password not more than 10 characters</strong></font><br>";
         valid = false;
    }
    else if(form.Password.value != form.Password2.value)
    {
        document.getElementById("password2error").innerHTML = "<font color='#FF0000'><strong>Password does not match</strong></font><br>";
         valid = false;
    }

    if(!(notLessThan(form.contact.value , 1)))
    {
        document.getElementById("contacterror").innerHTML = "<font color='#FF0000'><strong>Contact Name is required</strong></font><br>";
        valid = false;
    }
    else if(!(notMoreThan(form.contact.value , 30)))
    {
        document.getElementById("contacterror").innerHTML = "<font color='#FF0000'><strong>Contact Name not more than 30 characters</strong></font><br>";
        valid = false;
    }

    if(!(notLessThan(form.WebsiteInfo.value , 1)))
    {
        document.getElementById("webinfoerror").innerHTML = "<font color='#FF0000'><strong>WebSite Name is required</strong></font><br>";
        valid = false;
    }
	
	if(!(notLessThan(form.WebsiteURL.value , 1)))
    {
        document.getElementById("weburlerror").innerHTML = "<font color='#FF0000'><strong>Website URL is required</strong></font><br>";
        valid = false;
    }
	else if(form.WebsiteURL.value.substr(0,7) != 'http://' && form.WebsiteURL.value.substr(0,8) != 'https://'){
        document.getElementById("weburlerror").innerHTML = "<font color='#FF0000'><strong>Website URL is not valid</strong></font><br>";
        valid = false;
	}

	if(form.Country.options[0].selected)
    {
        document.getElementById("countryerror").innerHTML = "<font color='#FF0000'><strong>Country is required</strong></font><br>";
         valid = false;
    }

	if(form.Email.value.length < 1)
    {
        document.getElementById("emailerror").innerHTML = "<font color='#FF0000'><strong>Email is required</strong></font><br>";
         valid = false;
    }
    else if(!(isEmailAddr(form.Email.value)))
    {
        document.getElementById("emailerror").innerHTML = "<font color='#FF0000'><strong>Email is not valid</strong></font><br>";
         valid = false;
    }

    if(valid)
    {
		return true;
    }
    else
    {
		return false;
    }
}

//Validate Feedback Form
function validateFeedbackForm(form)
{
    var valid = true;
    document.getElementById("nameerror").innerHTML = "<font color='#FF0000'>&nbsp;</font><br>";
	document.getElementById("emailerror").innerHTML = "<font color='#FF0000'>&nbsp;</font><br>";
	document.getElementById("weburlerror").innerHTML = "<font color='#FF0000'>&nbsp;</font><br>";

//	RT_stripspaces(form.Name , 2);
	if(!(notLessThan(form.Name.value , 1)))
    {
        document.getElementById("nameerror").innerHTML = "<font color='#FF0000'><strong>Please Provide a Name</strong></font><br>";
        valid = false
    }

	if(notLessThan(form.WebsiteURL.value , 1) && form.WebsiteURL.value.substr(0,7) != 'http://' && form.WebsiteURL.value.substr(0,8) != 'https://'){
        document.getElementById("weburlerror").innerHTML = "<font color='#FF0000'><strong>Website URL is not valid</strong></font><br>";
        valid = false;
	}

	if(form.Email.value.length < 1)
    {
        document.getElementById("emailerror").innerHTML = "<font color='#FF0000'><strong>Email is required</strong></font><br>";
         valid = false;
    }
    else if(!(isEmailAddr(form.Email.value)))
    {
        document.getElementById("emailerror").innerHTML = "<font color='#FF0000'><strong>Email is not valid</strong></font><br>";
         valid = false;
    }
	
	if(!(notLessThan(form.Phone.value , 1)))
    {
        document.getElementById("phoneerror").innerHTML = "<font color='#FF0000'><strong>Please Provide a Phone Number</strong></font><br>";
        valid = false
    }
	
    if(valid)
    {
		return true;
    }
    else
    {
		return false;
    }
}
