
function select_language(lng) {
    var URL = location.href;
    var hasLanguage = URL.indexOf('lang=');
    if(hasLanguage != -1){
      URL = URL.slice(0, hasLanguage -1);
    }
    var hasParams = URL.indexOf('?');
    var sep;
    if(hasParams != -1)
      sep = '&';
    else
      sep = '?';

    if (lng=='en') {
      URL = URL + sep + 'lang=en';
    } else if(lng=='ro'){
      URL = URL + sep + 'lang=ro';
    }
    location.href = URL;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}



function popupLoad(action) {
    window.open(action,"popup","statusbar=no,menubar=no,scrollbars=yes,width=480,height=420,top=50,left=50");
}

/**
*  has reached max legth?
**/
function isMax(elmnt, content){
    if (content.length==elmnt.maxLength){
        alert("The code field must have a maximum length of " + elmnt.maxLength + " characters.");
        return true;
    }
    return false;
}

function isMin(elmnt, content){
    if (content.length<3){
        alert("The code field must have a minimum length of 3 characters.");
        return false;
    }
        //return true;
}

/**
*  starts with 0?
**/
function startsWith(elmnt, content){
    if (content.charAt(0)=='0'){
        alert("The code field can not start with 0");
        return true;
    }
}

function isValid(elmnt, content){
    if(startsWith(elmnt, content)==true){
        //alert("Code field can not start with 0");
        return false;
    }
    if(isMax(elmnt, content)==true){
        //alert("The code field must have a maximum length of " + elmnt.maxLength + " characters.");
        return false;
    }
    //if (IsShortCode(content)==false)
    //    alert("Code value is not short code. Put '*' symbol first.");
}

function validate(form){
    if(startsWith(form.code, form.code.value)==true){
        alert("Code field can not start with 0");
        return false;
    }
    if(isMin(form.code, form.code.value)==false)
        return false;
}
//1. Returns the current date in mm/dd/yyyy format
function now() 
{ 
	var stDate; 
	var now; 
	now_date= new Date(); 
	stDate=(now_date.getMonth()+1)+"/"+now_date.getDate()+"/"+now_date.getYear(); 
	return(stDate); 
} 

// populate zone dependend from select service type
//global variable
function populateZone1(selectObjFrom, selectObjTo, selectedValueFrom, selectedValueTo) {
 var OptLen = 0;
 var choosenValue=selectedValueFrom;
 var i=0;
 var j=0;
 for(j=selectObjTo.length-1;j>=0;j--)
    selectObjTo.options[j]=null;
 //National
 if(choosenValue==0) {
   selectObjTo.disabled = false;
   selectObjTo.options[0] = new Option;
   selectObjTo.options[0].value="0";
   selectObjTo.options[0].text="National (0)";
 }
 //Locale
 else if(choosenValue==1) {
   selectObjTo.disabled = false;
   selectObjTo.options[0] = new Option;
   selectObjTo.options[0].value=(-1);
   selectObjTo.options[0].text=selectZoneStr;
   for(j=0;j<zoneValues.length;j++){
    selectObjTo.options[j+1] = new Option;
    selectObjTo.options[j+1].value=zoneValues[j];
    selectObjTo.options[j+1].text=zoneStrings[j];
   }
   if(selectedValueTo!='-1')
    selectObjTo.selectedIndex = selectedValueTo;
 }
 else{
   selectObjTo.options[0] = new Option;
   selectObjTo.options[0].value=(-1);
   selectObjTo.options[0].text=selectZoneStr;
   /*for(j=0;j<zoneValues.length;j++){
    selectObjTo.options[j+1] = new Option;
    selectObjTo.options[j+1].value=zoneValues[j];
    selectObjTo.options[j+1].text=zoneStrings[j];
   } */
   selectObjTo.disabled = true;
 }

}

//1. Determines if a string has a valid email structure
function IsEmail(sFieldValue)
{
	var checkEmail = "@.";
	var checkStr = sFieldValue;
	var EmailValid = false;
	var EmailAt = false;
	var EmailPeriod = false;

	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkEmail.length;  j++)
		{
			if (ch == checkEmail.charAt(j) && ch == "@")
				EmailAt = true;
			if (ch == checkEmail.charAt(j) && ch == ".")
				EmailPeriod = true;
			if (EmailAt && EmailPeriod)
				break;
			if (j == checkEmail.length)
				break;
		}
		// if both the @ and . were in the string
		if (EmailAt && EmailPeriod)
		{
			EmailValid = true
			break;
		}
	}

	return(EmailValid);
}

//1. Determines if a string is completely numeric
function isNumeric(sFieldValue)
{
	var checkOK = "0123456789";
	var checkStr = sFieldValue;
	var allValid = true;

	if (checkStr=="")
	{
		allValid = false;
	}

	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);

		for (j = 0;  j < checkOK.length;  j++)
		{
			if (ch == checkOK.charAt(j))
				break;
		}
		
		if (j == checkOK.length)
		{
			allValid = false;
		}
	}

	if(allValid==false){
	    alert("You must enter only numeric values");
	    return false;
	}
	return true;
}

//1. Determines if a string is short-code number
function IsShortCode(sFieldValue)
{
	var checkStr = sFieldValue;
	var aValid = false;

      if(sFieldValue==null || sFieldValue.length<=0) return(aValid);
	ch = checkStr.charAt(0);
      if (ch == "*")
		{
			aValid = true;
		}
	return(aValid);
}

//1. Determine is a field value is numeric or an email
//2. Checks if the field is required
//3. Checks if the field length has exceeded the maximum size
function FieldValidation(sFieldName, sFieldValue, sRule, bRequired, maxLength)
{
	var sErrorMessage;
	
	sErrorMessage="";

	switch(sRule)
	{
		case "Numeric":
			if (sFieldValue.length>0 && IsNumeric(sFieldValue)==false)
			{
				sErrorMessage+=sFieldName + " is not numeric.";
			}
			break;
		case "Email":
			if (IsEmail(sFieldValue)==false)
			{
				sErrorMessage+=sFieldName + " is not an email.";
			}
			break;
		case "ShortCode":
			if (IsShortCode(sFieldValue)==false)
			{
				sErrorMessage+=sFieldName + " is not short code. Put '*' first symbol.";
			}
			break;
	}

	/*------------------------------------------check if field is required---------------------------*/
	if ( (bRequired==true) && (sFieldValue!=""))
	{
		if (sFieldValue.length==0)
		{
			sErrorMessage+=sFieldName + " is required.";
		}
	}	
	else if ( (bRequired==true) && (sFieldValue==""))
	{
		sErrorMessage+=sFieldName + " is required.";
	}
	/*------------------------------------check for maxlength--------------------------------------*/
	if ((sFieldValue!="") && (maxLength>0))
	{
		if (sFieldValue.length>maxLength)
		{
			//sErrorMessage+=sFieldName + " exceeded the maximum length of " + maxLength + " characters <br>";
			sErrorMessage+=sFieldName + " exceeded the maximum length of " + maxLength + " characters.";

		}
	}

	//return(sErrorMessage);
	alert(sErrorMessage);
}

//1. Displays the error message in a popup window
function displayErrorMessage(sErrorMsg) 
{
//win = window.open(",", 'popup', 'height = 200 width=200 toolbar = yes  titlebar=yes status = no resizeable=yes scrollbars=yes');
//win.document.write("Data Validation Errors<br>");
//win.document.write("" + sErrorMsg + "");
alert("Error: "+sErrorMsg);
}
