/********************************
 *		Common Functions		*
 ********************************/
 function my_xmlHttpObject () {
          var xml_http_object;
          try {
	         xml_http_object = new XMLHttpRequest();
          } catch(e) {
              try {
                  xml_http_object = new ActiveXObject("Msxml2.XMLHTTP");
              } catch(e) {
                  try {
                      xml_http_object = new ActiveXObject("Microsoft.XMLHTTP");
                  } catch(e) {
                      alert("ERROR: Your browser does not support AJAX.");
                      return false;
                  }
              }
          }
	     return xml_http_object;
      }          

function display_comments_ajax(valid_domain,form_element){
        var button_obj = document.getElementById("btnAdd");
        button_obj.disabled = true;
        xmlHttp=my_xmlHttpObject();
        if (xmlHttp==null){
          alert ("Your browser does not support AJAX!");
          return;
        }
        form_element_comment = form_element +"_comment";
        if(form_element == 'spf_a_others') {append = "a";}
        if(form_element == 'spf_mx_others') {append = "mx";}
        if(form_element == 'spf_ptr_others') {append = "ptr";}
        if(form_element == 'spf_exists_others') {append = "exists";}
        if(form_element == 'spf_include') {append = "include";}
        var url="domain_exist_ajax.php";
        url=url+"?domains="+valid_domain+"&append="+append;
        url=url+"&sid="+Math.random();
        xmlHttp.onreadystatechange=function(){stateChanged(valid_domain,form_element);};
        xmlHttp.open("GET",url,true);
        xmlHttp.send(null);
}
function stateChanged(valid_domain,form_element){
   if (xmlHttp.readyState==4){
        var text = xmlHttp.responseText;
        var displayText = displayText1 = displayText2 = displayText3 = "";
        var form_element_comment = form_element +"_comment";
        var arr_option = text.split("|");
        var displayText = displayText1 = displayText2 = "";
        if (arr_option[0]) displayText = "Invalid domain name: "+arr_option[0];
        if (arr_option[1]) displayText1= "DNS record not present: "+arr_option[1];
        if (arr_option[2]) displayText2= "valid domains: "+arr_option[2];
        if (arr_option[3]) displayText3= arr_option[3];
        if (displayText1) displayText+="<br>"+displayText1;
        if( arr_option[0] ||  arr_option[1]) {
                document.getElementById(form_element_comment).innerHTML=displayText;
        }
        else{
                document.getElementById(form_element_comment).innerHTML="";
        }
        document.getElementById(form_element).value = displayText3;
        form_spf_record();
        var button_obj = document.getElementById("btnAdd");
        button_obj.disabled = false;
   }
}
function form_spf_record(){
        var spf_obj = document.getElementById("dcnamean");
        var spf_value = "v=spf1";
        if(document.getElementById("spf_a") != null){
                for (var i=0; i<document.dns.spf_a.length; i++){
                        if (document.dns.spf_a[i].checked) {spf_a = document.dns.spf_a[i].value;}
                }
                if(spf_a == 'yes') spf_value+=" a";
        }
        if(document.getElementById("spf_mx") != null){
                for (var i=0; i<document.dns.spf_mx.length; i++){
                        if (document.dns.spf_mx[i].checked) {spf_mx = document.dns.spf_mx[i].value;}
                }
                if(spf_mx == 'yes') spf_value+=" mx";
        }
        if(document.getElementById("spf_ptr") != null){
                for (var i=0; i<document.dns.spf_ptr.length; i++){
                        if (document.dns.spf_ptr[i].checked) {spf_ptr = document.dns.spf_ptr[i].value;}
                }
                if(spf_ptr == 'yes') spf_value+=" ptr";
        }

        var spf_a_others = trim(document.getElementById("spf_a_others").value);
        spf_value+=list_to_spf(spf_a_others,"a");

        var spf_mx_others = trim(document.getElementById("spf_mx_others").value);
        spf_value+=list_to_spf(spf_mx_others,"mx");

        var spf_ptr_others = trim(document.getElementById("spf_ptr_others").value);
        spf_value+=list_to_spf(spf_ptr_others,"ptr");

        var spf_exists_others = trim(document.getElementById("spf_exists_others").value);
        spf_value+=list_to_spf(spf_exists_others,"exists");

        var spf_include = document.getElementById("spf_include").value;
        spf_value+=list_to_spf(spf_include,"include");

        var spf_ip_subnet = document.getElementById("spf_ip_subnet").value;
        spf_value+=list_to_spf(spf_ip_subnet,"ip4");

        var spf_ip6_subnet = document.getElementById("spf_ip6_subnet").value;
        spf_value+=list_to_spf(spf_ip6_subnet,"ip6");

        if(document.getElementById("spf_all") != null){
                for (var i=0; i<document.dns.spf_all.length; i++){
                        if (document.dns.spf_all[i].checked) {spf_all = document.dns.spf_all[i].value;}
                }
                if(spf_all == 'soft') spf_value+=" ~all";
                if(spf_all == 'hard') spf_value+=" -all";
                if(spf_all == 'neutral') spf_value+=" ?all";
        }

        spf_obj.value = spf_value;
}

function list_to_spf(space_seperated, type){
        var spf_list = "";
        if(space_seperated){
        var space_seperated_array = space_seperated.split(" ");
        var part_num=0;
        while (part_num < space_seperated_array.length){
                spf_list+= " "+type+":"+space_seperated_array[part_num];
                part_num+=1;
        }
        }
        return spf_list;
}

function isValidIP6(ipaddr){
        var regExp = /^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){6}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(([0-9A-Fa-f]{1,4}:){0,5}:((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|(::([0-9A-Fa-f]{1,4}:){0,5}((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}(\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|(::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:))$/;
        /*var regExp = "/^(
                           (([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|
                           (([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|
                           (([0-9A-Fa-f]{1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})|
                           (([0-9A-Fa-f]{1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})|
                           (([0-9A-Fa-f]{1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})|
                           (([0-9A-Fa-f]{1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})|
                           (([0-9A-Fa-f]{1,4}:){6}
                                ((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}
                                (\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|
                           (([0-9A-Fa-f]{1,4}:){0,5}:
                                ((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}
                                (\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|
                           (::([0-9A-Fa-f]{1,4}:){0,5}
                                ((\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b)\.){3}
                                (\b((25[0-5])|(1\d{2})|(2[0-4]\d)|(\d{1,2}))\b))|
                           ([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})|
                           (::([0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})|
                           (([0-9A-Fa-f]{1,4}:){1,7}:))$/";*/
        if (ipaddr.search(regExp)==-1){
            return false;
        }
        else{
                return true;
        }
}

// TXT Record IP Validate added by prakash 06/11/2007
function isValidIPAddress(ipaddr,type) {
    if(type == 'subnet'){
        var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/\d{1,2}$/;
        var array_prefix = ipaddr.split("/");
        if(array_prefix[1] > 32) { 
		return false;}
    }
    else{
        var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
   }
   if (re.test(ipaddr)) {
      var parts = ipaddr.split(".");
      if (parseInt(parseFloat(parts[0])) == 0) {
            return false;
         }
      for (var i=0; i<parts.length; i++) {
         if (parseInt(parseFloat(parts[i])) > 255) {
            return false;
           }
      }
      return true;

   } else {
      return false;
   }
}

                                                                         
 //To clear all text boxes in a form
 function clearForm(formIdent) 
{ 
  var form, elements, i, elm; 
  form = document.getElementById 
    ? document.getElementById(formIdent) 
    : document.forms[formIdent]; 

	if (document.getElementsByTagName)
	{
		elements = form.getElementsByTagName('input');
		for( i=0, elm; elm=elements.item(i++); )
		{
			if (elm.getAttribute('type') == "text")
			{
				elm.value = '';
			}
			if (elm.getAttribute('type') == "password")
			{
				elm.value = '';
			}
		}
	}

	// Actually looking through more elements here
	// but the result is the same.
	else
	{
		elements = form.elements;
		for( i=0, elm; elm=elements[i++]; )
		{
			if (elm.type == "text")
			{
				elm.value ='';
			}
			if (elm.type == "password")
			{
				elm.value = '';
			}
		}
	}
}
function verifyIP(IPvalue)
	{
	var errorString = "";
	var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
	var ipArray = IPvalue.match(ipPattern);
	if (IPvalue == "0.0.0.0" || IPvalue == "255.255.255.255")
		{
		return false;
		}
	if (ipArray == null)
		{
		errorString = errorString + ": "+IPvalue+" is not a valid IP address.";
		}
	else
		{
		for (i = 1; i < 5; i++)
			{
			thisSegment = ipArray[i];
			if (thisSegment > 255)
				{
				errorString = errorString + ": "+IPvalue+" is not a valid IP address.";
				i = 5;
				}
			if ((i == 1) && (thisSegment > 255))
				{
				errorString = errorString + ": "+IPvalue+" is a specialddress and cannot be used here.";
				i = 5;
				}
			}
		}
	extensionLength = 3;
	if (errorString == "")
		{
		return true;
		}
	else
		{
		return false;
		}
	}
function caps(strvalue)
	{
	slen=strvalue.length;
	flag = true;
	if((strvalue.substring(0,1)==" ") || (strvalue.substring(slen-1,slen)== " "))
		{
		flag = false;
		return(flag);
		}
	for (var ic = 0;ic < slen;ic ++)
		{
		if(strvalue.substring(ic,ic+1)>="A" && strvalue.substring(ic,ic+1)<="Z")
			{
			flag = false;
			}
		}
	return(flag);
	}
function validint(strvalue)
	{
	slen=strvalue.length;
	flag = true;
	if( (slen>6) || (strvalue.substring(0,1)==" ") || (strvalue.substring(slen-1,slen)== " ") )
		{
		flag = false;
		return(flag);
		}
	for (var ic=0;ic<slen;ic++)
		{
		if(strvalue.substring(ic,ic+1)>="0"&&strvalue.substring(ic,ic+1)<="9")
			{
			}
		else
			{
			flag = false;
			}
		}
	return(flag);
	}
function pipedollar(strvalue)
	{
	slen=strvalue.length;
	flag = true;
	if((strvalue.substring(0,1)==" ") || (strvalue.substring(slen-1,slen)== " "))
		{
		flag = false;
		return(flag);
		}
	for (var ic = 0;ic < slen;ic ++)
		{
		if(strvalue.substring(ic,ic+1)=="\|" || strvalue.substring(ic,ic+1)=="\$")
			{
			flag = false;
			}
		}
	return(flag);
	}


//added by ambika
//for cname validation
function validcname(strvalue)
{
//	alert("cname"); return false;
        slen=strvalue.length;
        flag = true;
        //if((strvalue.substring(0,1)==" ") || (strvalue.substring(slen-1,slen)== " "))
        if((strvalue.substring(0,1)=="-") || (strvalue.substring(slen-1,slen)== "-") ||(strvalue.substring(2,3)== "-") || (strvalue.substring(3,4)== "-") || (slen <6) || (slen >66))
                {
                flag = false;
                return(flag);
                }
        for (var ic = 0;ic < slen;ic ++)
                {
                if(strvalue.substring(ic,ic+2) == '--')
                {
                        flag = false;
                }
                var fc = strvalue.substring(ic,ic+1);
                if((fc >= 'a' && fc <= 'z') || (fc >= 'A' && fc <= 'Z') ||(fc >= '0' && fc <= '9') || fc == '-' || fc == '.' || fc == '/' || fc == '_')
                {
                }
                else
                {
                        flag = false;
                }
                }
        return(flag);
}

//for url forward validation
function validurlforward(strvalue)
{
//alert('hai');
	slen=strvalue.length;
	flag = true;
	//if((strvalue.substring(0,1)==" ") || (strvalue.substring(slen-1,slen)== " "))
	if((strvalue.substring(0,1)=="-") || (strvalue.substring(slen-1,slen)== "-") || (strvalue.substring(0,1)==".") || (strvalue.substring(slen-1,slen)== ".")  /*||(strvalue.substring(2,3)== "-") || (strvalue.substring(3,4)== "-")*/ || (slen <6) || (slen >66))
		{
		flag = false;
		return(flag);
		}
	for (var ic = 0;ic < slen;ic ++)
		{
		if(strvalue.substring(ic,ic+2) == '--')
		{
			flag = false;	
		}
		var fc = strvalue.substring(ic,ic+1);
		if((fc >= 'a' && fc <= 'z') || (fc >= 'A' && fc <= 'Z') ||(fc >= '0' && fc <= '9') || fc == '-' || fc == '.' || fc == '/' || fc == '_')
		{
		}
		else
		{
			flag = false;
		}
		}
	return(flag);
}
//for domain name validation
function validdomain(strvalue)
	{
	slen=strvalue.length;
	flag = true;
	//if((strvalue.substring(0,1)==" ") || (strvalue.substring(slen-1,slen)== " "))
	//if((strvalue.substring(0,1)=="-") || (strvalue.substring(slen-1,slen)== "-") ||(strvalue.substring(2,3)== "-") || (strvalue.substring(3,4)== "-") || (slen <6) || (slen >66))
	if((strvalue.substring(0,1)=="-") || (strvalue.substring(slen-1,slen)== "-") ||(slen <6) || (slen >66))
		{
		flag = false;
		return(flag);
		}
	for (var ic = 0;ic < slen;ic ++)
		{
		if(strvalue.substring(ic,ic+2) == '--')
		{
			flag = false;	
		}
		var fc = strvalue.substring(ic,ic+1);
		if((fc >= 'a' && fc <= 'z') || (fc >= 'A' && fc <= 'Z') ||(fc >= '0' && fc <= '9') || fc == '-' || fc == '.')
		{
		}
		else
		{
			flag = false;
		}
		}
	return(flag);
	}
	
	function fn_chooseform(get_type,get_package)
		{
			with(document.frm_product)	
			{
				hid_packagetype.value = get_package;
				action=get_type;
				submit();		
			}
		}
		
		function setAdminDetails(val)
{
	if (val==0)
	{
		document.frmSubmitTradeMark.txtmand_ACName.value = document.frmSubmitTradeMark.txtmand_RCName.value;
		document.frmSubmitTradeMark.txtACOrgName.value = document.frmSubmitTradeMark.txtRCOrgName.value;
		document.frmSubmitTradeMark.txtmand_ACStreet.value = document.frmSubmitTradeMark.txtmand_RCStreet.value;
		document.frmSubmitTradeMark.txtACStreet2.value = document.frmSubmitTradeMark.txtRCStreet2.value;
		document.frmSubmitTradeMark.txtmand_ACCity.value = document.frmSubmitTradeMark.txtmand_RCCity.value;
		document.frmSubmitTradeMark.txtACState.value = document.frmSubmitTradeMark.txtRCState.value;
		document.frmSubmitTradeMark.txtmand_ACPostalCode.value = document.frmSubmitTradeMark.txtmand_RCPostalCode.value;	
		document.frmSubmitTradeMark.cmbmand_ACCountry.value = document.frmSubmitTradeMark.cmbmand_RCCountry.value;
		document.frmSubmitTradeMark.txtmand_ACPhone.value = document.frmSubmitTradeMark.txtmand_RCPhone.value;
		document.frmSubmitTradeMark.txtmand_ACEMail.value = document.frmSubmitTradeMark.txtmand_RCEMail.value;
		return true;
	}
	else
	{
		document.frmSubmitTradeMark.txtmand_ACName.value = ""
		document.frmSubmitTradeMark.txtACOrgName.value = ""
		document.frmSubmitTradeMark.txtmand_ACStreet.value = ""
		document.frmSubmitTradeMark.txtACStreet2.value = ""
		document.frmSubmitTradeMark.txtmand_ACCity.value = ""
		document.frmSubmitTradeMark.txtACState.value = ""
		document.frmSubmitTradeMark.txtmand_ACPostalCode.value = ""
		document.frmSubmitTradeMark.cmbmand_ACCountry.value = ""
		document.frmSubmitTradeMark.txtmand_ACPhone.value = ""
		document.frmSubmitTradeMark.txtmand_ACEMail.value = ""
	}
	return true;
}
function setTechDetails(val)
{
	if (val==0)
	{
		document.frmSubmitTradeMark.txtmand_TCName.value = document.frmSubmitTradeMark.txtmand_RCName.value;
		document.frmSubmitTradeMark.txtTCOrgName.value = document.frmSubmitTradeMark.txtRCOrgName.value;
		document.frmSubmitTradeMark.txtmand_TCStreet.value = document.frmSubmitTradeMark.txtmand_RCStreet.value;
		document.frmSubmitTradeMark.txtTCStreet2.value = document.frmSubmitTradeMark.txtRCStreet2.value;
		document.frmSubmitTradeMark.txtmand_TCCity.value = document.frmSubmitTradeMark.txtmand_RCCity.value;
		document.frmSubmitTradeMark.txtTCState.value = document.frmSubmitTradeMark.txtRCState.value;
		document.frmSubmitTradeMark.txtmand_TCPostalCode.value = document.frmSubmitTradeMark.txtmand_RCPostalCode.value;	
		document.frmSubmitTradeMark.cmbmand_TCCountry.value = document.frmSubmitTradeMark.cmbmand_RCCountry.value;
		document.frmSubmitTradeMark.txtmand_TCPhone.value = document.frmSubmitTradeMark.txtmand_RCPhone.value;
		document.frmSubmitTradeMark.txtmand_TCEMail.value = document.frmSubmitTradeMark.txtmand_RCEMail.value;
	}
	else if (val==1)
	{
		document.frmSubmitTradeMark.txtmand_TCName.value = document.frmSubmitTradeMark.txtmand_ACName.value;
		document.frmSubmitTradeMark.txtTCOrgName.value = document.frmSubmitTradeMark.txtACOrgName.value;
		document.frmSubmitTradeMark.txtmand_TCStreet.value = document.frmSubmitTradeMark.txtmand_ACStreet.value;
		document.frmSubmitTradeMark.txtTCStreet2.value = document.frmSubmitTradeMark.txtACStreet2.value;
		document.frmSubmitTradeMark.txtmand_TCCity.value = document.frmSubmitTradeMark.txtmand_ACCity.value;
		document.frmSubmitTradeMark.txtTCState.value = document.frmSubmitTradeMark.txtACState.value;
		document.frmSubmitTradeMark.txtmand_TCPostalCode.value = document.frmSubmitTradeMark.txtmand_ACPostalCode.value;
		document.frmSubmitTradeMark.cmbmand_TCCountry.value = document.frmSubmitTradeMark.cmbmand_ACCountry.value;
		document.frmSubmitTradeMark.txtmand_TCPhone.value = document.frmSubmitTradeMark.txtmand_ACPhone.value;
		document.frmSubmitTradeMark.txtmand_TCEMail.value = document.frmSubmitTradeMark.txtmand_ACEMail.value;
	}
	else
	{	
		document.frmSubmitTradeMark.txtmand_TCName.value = ""
		document.frmSubmitTradeMark.txtTCOrgName.value = ""
		document.frmSubmitTradeMark.txtmand_TCStreet.value = ""
		document.frmSubmitTradeMark.txtTCStreet2.value = ""
		document.frmSubmitTradeMark.txtmand_TCCity.value = ""
		document.frmSubmitTradeMark.txtTCState.value = ""
		document.frmSubmitTradeMark.txtmand_TCPostalCode.value = ""
		document.frmSubmitTradeMark.cmbmand_TCCountry.value = ""
		document.frmSubmitTradeMark.txtmand_TCPhone.value = ""
		document.frmSubmitTradeMark.txtmand_TCEMail.value = ""
	}
	return true;
}
/*
 * used to clear both forms in Contact Update
 */
function fn_clear()
{
	with(frmSubmitTradeMark)
	{
		setAdminDetails(2);
		setTechDetails(2);
	}
}

function getQueryString( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

