
//Function to open a window, file name corresponds to the window location.
function openWin(URL) 
    {
        var win;
		win = window.open(URL, "", "resizable=0,toolbar=0,location=0,directories=0,status=0,scrollbars=0,menubar=0,width=622,height=535"); 
    }    

	function validateURLIfNotEmpty(item){
		var strURL = item.getValue();
		if (strURL.length > 0)
		{
			validateURL(item);
		}
		else
		{
			item.setValid(true);
					return true;
		}

	}

	//validate URL 
	function validateURL(item) {		
		var strURL = item.getValue();
		strURL = strURL.toUpperCase();
		var strhttp;
		var strhttps;			
		var strDomain;	
		var strChk = 0;
		
		strhttp		= strURL.substr(0,7);
		strhttps	= strURL.substr(0,8);			
		
		//Check if len of URL is less than 8 or if value is not http:// or https:// .
		if (strURL.length < 8){
			strChk = 1;							
		}
		else if (!((strhttp == "HTTP://") || (strhttps == "HTTPS://") )){					
			strChk = 1;									
		}
		
		
		//If above are valid then check for the remaining URL (URL-protocol)
		if (strChk != 1){
			

			if (strhttp == "HTTP://") strDomain = (strURL.substr(strhttp.length,strURL.length));
			if (strhttps == "HTTPS://") strDomain = (strURL.substr(strhttps.length,strURL.length));		
			

			//Check if first character is alpha numberic or not
			if (!(isAlphaNumeric(strDomain.substr(0,1)))) strChk = 1;			
			
			//Check if / is ther in URL if present then get string before /.
			if ((strDomain.indexOf("/")) != "-1") strDomain = strDomain.substr(0,strDomain.indexOf("/"))
		
			//Check if . is there in the string.
			if ((strDomain.indexOf(".")) == "-1") strChk = 1;
			
			//Check to see if characters are present between . and /								
			strDomChk = strDomain.substr(0,strDomain.lastIndexOf("."));
			strDotChk = strDomain.substr(strDomain.lastIndexOf(".")+1, strDomain.length);				
			
			//Check to see if there are less than 1 characters bet start of the string and .							
			if (strDomChk.length < 1) strChk = 1;				
		
			//Check to see if there are less than 2 characters bet start of the . and end of string.
			if (strDotChk.length < 2) strChk = 1;
			
			//to check the last characters of string after last . to be only characters (com, in, net, org ...)	
			if (!(isAlpha(strDotChk))) strChk = 1;				
		}
		
		if (strChk == 1){			
			alert("Please enter a valid URL");
			item.setValid(false);
			return false;

		}
		else
		{	
			item.setValid(true);			
			return true;
		}

	}


	//check that all values in the string contains Alpha Numeric values
	function isAlphaNumeric(val)
	{
		if (val.match(/^[a-zA-Z0-9]+$/))
		{
			return true;
		}
		else
		{
			return false;
		}	
	}
	
	//check that all values in the string contains only Alphabets
	function isAlpha(val)
	{
		if (val.match(/^[a-zA-Z]+$/))
		{
			return true;
		}
		else
		{
			return false;
		}	
	}

	//Check the lenght of description field
	function validateIntroduction(item) {

	    var dctitem = item.getName();
	    var dctitemlen = item.getValue().length;
	//  alert (dctitemlen);
	
	    //if (dctitem == '/District_PSA/psa/description'){
	    //    var maxChars = 500;
	    //}  

	    var maxChars = 500;
	    
	 // alert(validateMaxChar(dctitemlen, maxChars));

	    if (validateMaxChar(dctitemlen, maxChars)==false) {
	  
	      item.setValid(false);
	     // alert ("False");
	     	item.setFocus();
	        
	    }
	    else{
              item.setValid(true); 
           //  alert ("True");
           //   return true;
           //	IWDatacapture.save();
	    }
        }
	    
	  //validates the maximum character length allowed in text area
	  
         function validateMaxChar(dctField, maxlen){
	      
	        var diff = maxlen - dctField
		var result = true;
            	var a=Math.max(0,diff)
			if (a==0)
			{
			   var diff1= -diff
			}
			else{
			   var diff1= diff
			}            
                
			if (dctField > maxlen){
			   alert ("Text is limited to " + maxlen + " characters. You got " + diff1 + " extra character(s).")
					   result = false;	
			}  		
		   return result;
		}	

