// Hassan Kanj
var required_Form_Fields = ['firstnametxt','lastnametxt','emailtxt','mobiletxt','countrycmb','usernametxt','passtxt','confirmpasstxt','accounttypecmb','paymentcmb'];

function validate_register_form(formObj,email,pass,confirmpass,username,mobile,account_type)
{
	//only company name is a required field when user select a business account
	if(account_type=='business' && document.registerfrm.companytxt.value =='')
	{
        alert ( "Please enter your company name." );
        return false;		
	}
	
	if ( formObj.termschk.checked == false )
    {
        alert ( "Please check the Terms & Conditions box." );
        return false;
    }

	if ( formObj.credittypecmb.selectedIndex == 0 )
    {
        alert ( "Please select credits amount" );
        return false;
    }
	
	if(!IsNumeric(mobile))
	{
			alert("mobile number must be numeric");
			return false;
	}

	if(pass != confirmpass)
	{
			alert("password and confirm password aren't the same");
			return false;
	}
	
	if(username.length < 5 || username.length > 10 )
	{
			alert("username must be between 5 and 10 characters");
			return false;
	}
	
	
		if(pass.length < 5 || pass.length > 10 )
	{
			alert("password must be between 5 and 10 characters");
			return false;
	}

    for (var i=0; i<required_Form_Fields.length; i++)
    {
        if (!formObj[required_Form_Fields[i]].value)
        {
            alert('Please fill all the required fields.');
            return false;
        }
    }
	
	if(!echeck(email))
	{
		return false;
	}

	return true;
	
	
}


/**
 * DHTML email validation script.
 */

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail address")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail address")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail address")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail address")
		    return false
		 }

 		 return true					
	}


//check if username exist using ajax
function checkUserAvailability(user,result_div)
{
 if(user=="") 
 {
	 return;
 }
 //show the waiting icon
  result_div.innerHTML="<img src=\"./images/ajaxwait.gif\" /> ";
  var _value=user;
  var xmlHttp;
  try
  {
    /* Firefox, Opera 8.0+, Safari */
    xmlHttp=new XMLHttpRequest();
  }
  catch (e)
  {
    /* newer IE */
    try
    {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      /* older IE */
      try
      {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        alert("Your browser is old and does not have AJAX support!");
        return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
  {
    if(xmlHttp.readyState==4)
    {
	  result_div.innerHTML="";
      result_div.innerHTML  +=xmlHttp.responseText;
    }
  }
  xmlHttp.open("GET","checkusername.php?value="+_value,true);
  xmlHttp.send(null);
}


//check if email exist using ajax
function checkEmailAvailability(user,result_div)
{
 if(user=="") 
 {
	 return;
 }
 //show the waiting icon
  result_div.innerHTML="<img src=\"./images/ajaxwait.gif\" /> ";
  var _value=user;
  var xmlHttp;
  try
  {
    /* Firefox, Opera 8.0+, Safari */
    xmlHttp=new XMLHttpRequest();
  }
  catch (e)
  {
    /* newer IE */
    try
    {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      /* older IE */
      try
      {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        alert("Your browser is old and does not have AJAX support!");
        return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
  {
    if(xmlHttp.readyState==4)
    {
	  result_div.innerHTML="";
      result_div.innerHTML  +=xmlHttp.responseText;
    }
  }
  xmlHttp.open("GET","checkemail.php?value="+_value,true);
  xmlHttp.send(null);
}



//if numeric

function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }


function print_total_sms(Credits_Per_Message,UserCredits)
{
	//if even 1 character is arabic consider 1 SMS -> 69 character 
	//else 1 SMS -> 159
	
	//if sending name with the msg [send name is checked] then automatically remove some chars from the SMS
	
	var Reserved_Chars_For_Name = 45;
	var SMS = document.sendfrm.msgtxt.value;
	var SMSLength = SMS.length;
	
	
	if(document.getElementById('sendnamechk'))
	{
		if(document.sendfrm.sendnamechk.checked)
		{
			//SMSLength =  SMSLength + Reserved_Chars_For_Name; 
		}
	}
	
	var Arabic       = false;

	
	//check 1st for any arabic character
	for(i = 0; i< SMSLength ; i++)
	{
		if (SMS.charCodeAt(i) >=1571 && SMS.charCodeAt(i) <=1610)
		{
			var divisionValue = 67;
			var divisionValue2 = 63; //nb of char in a single SMS after the 1st SMS [arabic]
			Arabic = true;
			
			//disable sending by name
			if(document.getElementById('sendnamechk'))
			{
				document.sendfrm.sendnamechk.checked = false;
		    }
			document.sendfrm.msgtxt.focus();
		}

  //now if there's no arabic character found
  if (Arabic==false)
  {
	  var divisionValue = 158;
	  var divisionValue2 = 145; //nb of char in a single SMS after the 1st SMS [english]
  }

//if sms length didn't exceed the allowed char for the 1st SMS total sms = 1
	}
	if(SMSLength <= divisionValue)
	{
		var TotalSMS = 1;
	}
	else
	{
		var length_without_1st_sms = SMSLength - divisionValue ;  //because after the 2nd SMS length will be divided by divisionValue2
		var TotalSMS = Math.ceil(length_without_1st_sms/divisionValue2) + 1 ; // + 1 <-- 1st SMS
	}
	
	if(isNaN(TotalSMS))
	{
		TotalSMS = 0;
	}
	
	if(Arabic == false)
	{
		document.getElementById('unicode').value = 'false' //set unicode [arabic] to false [used to change the sending link]
		document.getElementById('totalsmsdiv').innerHTML=SMSLength+" characters, "+TotalSMS+" SMS(s)";
	}
	else
	{
		document.getElementById('unicode').value = 'true' //set unicode [arabic] to true [used to change the sending link]
		document.getElementById('totalsmsdiv').innerHTML=SMSLength+" unicode characters, "+TotalSMS+" Arabic SMS(s)";
		
	}

	//now change the hidden field in the form that will store the SMS
	document.getElementById('totalsms').value = TotalSMS;
	
	//now check if user has enough credits to send more messages
	
	//you've to check if Credit Per SMS * Nb of SMS < total credits

	if(Credits_Per_Message*TotalSMS > UserCredits)
	{
		alert("You don't have enough credits to write more..");
		document.sendfrm.Submit.disabled = true; //disable sending
	}
	else
	{
		document.sendfrm.Submit.disabled = false; //enable sending button
	}
	
}












function print_total_sms_template()
{

	var SMS 		   = document.sendfrm.msgtxt.value;
	var SMSLength 	   = SMS.length;
	var divisionValue  = 158;
 	var divisionValue2 = 145; //nb of char in a single SMS after the 1st SMS [english]

	
	//check 1st for last arabic character
	for(i = 0; i< SMSLength ; i++)
	{
		//if last entered character is arabic 
		if (SMS.charCodeAt(SMSLength-1) >=1571 && SMS.charCodeAt(SMSLength-1) <=1610) //if arabic
		{
			//remove last arabic character
			document.sendfrm.msgtxt.value = document.sendfrm.msgtxt.value.substring(0, SMSLength-1);
			return false;
			
		}

	//check for any arabic character and remove it
	for(i = 0; i< SMSLength ; i++)
	{
		if (SMS.charCodeAt(i) >=1571 && SMS.charCodeAt(i) <=1610)
		{
			alert('You cannot write arabic characters please delete them');
		}

	}

//if sms length didn't exceed the allowed char for the 1st SMS total sms = 1
	}
	if(SMSLength <= divisionValue)
	{
		var TotalSMS = 1;
	}
	else
	{
		var length_without_1st_sms = SMSLength - divisionValue ;  //because after the 2nd SMS length will be divided by divisionValue2
		var TotalSMS = Math.ceil(length_without_1st_sms/divisionValue2) + 1 ; // + 1 <-- 1st SMS
	}
	
	if(isNaN(TotalSMS))
	{
		TotalSMS = 0;
	}

		document.getElementById('totalsmsdiv').innerHTML=SMSLength+" characters, "+TotalSMS+" SMS(s)";



	//now change the hidden field in the form that will store the SMS
	document.getElementById('totalsms').value = TotalSMS;
	
	
	
}



//


