function Trim(s) 
{
  // Remove leading spaces and carriage returns
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
  {
    s = s.substring(1,s.length);
  }
  // Remove trailing spaces and carriage returns
  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
  {
    s = s.substring(0,s.length-1);
  }
  return s;
}

//********************* START of function for email-id validation  ****************************//
function isValidEmail(emailStr) {
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) {
		alert("Email address seems incorrect (check @ and .'s)");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	// Start by checking that only basic ASCII characters are in the strings (0-127).
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			alert("Ths username contains invalid characters in e-mail address.");
			return false;
		}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			alert("Ths domain name contains invalid characters in e-mail address.");
			return false;
		}
	}
	if (user.match(userPat)==null) {
		alert("The username doesn't seem to be valid in e-mail address.");
		return false;
	}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("Destination IP address is invalid!");
				return false;
	   		}
		}
		return true;
	}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			alert("The domain name does not seem to be valid in e-mail address.");
			return false;
	   }	
	}
	if (checkTLD && domArr[domArr.length-1].length!=2 && 
		domArr[domArr.length-1].search(knownDomsPat)==-1) {
		alert("The address must end in a well-known domain or two letter " + "country.");
		return false;
	}

	if (len<2) {
		alert("This e-mail address is missing a hostname!");
		return false;
	}	
	return true;
}

function countInstances(string, word) {
  var substrings = string.split(word);
  return substrings.length - 1;
}

function isValidUrl(urlval)
{
	var y = countInstances(urlval, "http://");
	var z = countInstances(urlval, "https://");
	var p = countInstances(urlval, "ftp://");
	if(y==0 && z==0 && p==0)
	{
		alert("Please Enter \" http:// \" , \" https:// \"  or \" ftp:// \" in your URL");
		return false;
	}
	var x = countInstances(urlval, ".")
	if(x<1) 
	{
		alert("Please Enter the Proper URL");
		return false;
	}
	return true;
}
//********************* END of function for email-id validation  ****************************//

function usernameValidation(value,length)
{
	chk1="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_.@";
	for(i=0;i<length;i++)
	{
		ch1=value.charAt(i);
		rtn1=chk1.indexOf(ch1);
		if(rtn1==-1)
			return false;
	}
	return true;
}

function planValueValidation(value,length)
{
	chk1="0123456789.";
	for(i=0;i<length;i++)
	{
		ch1=value.charAt(i);
		rtn1=chk1.indexOf(ch1);
		if(rtn1==-1)
			return false;
	}
	return true;
}

function datedifferance(date1,date2)
{
	if(date1 == "")
	{
		var currentTime = new Date()
		var month = currentTime.getMonth() + 1
		var day = currentTime.getDate()
		var year = currentTime.getFullYear()
		var strDate1 = year + "/" + month + "/" + day;
	}
	else { var strDate1 = date1; }
	var strDate2 = date2;
	var difference = ((Date.parse(strDate2))-(Date.parse(strDate1)))/(24*60*60*1000).toFixed(0);
	return difference;
}

function datevalidator(year,month,day)
{
	if (month == 2) {
		if ((year % 4) == 0) { if (day > 29) { return 0; } }
		else { if (day > 28) { return 0; } }
	}
	else if (month == 4 || month == 6 || month == 9 || month == 11)	{ if (day > 30) { return 0; } }
	else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { if (day > 31) { return 0; } }
	else { return 1; }
}

function checknum(value,length)
{
	chk1="1234567890";
	for(i=0;i<length;i++)
	{
		ch1=value.charAt(i);
		rtn1=chk1.indexOf(ch1);
		if(rtn1==-1)
			return false;
	}
	return true;
}

function checknumhyphen(value,length) // Normally used for phone number validaiton
{
	chk1="1234567890- +()";
	for(i=0;i<length;i++)
	{
		ch1=value.charAt(i);
		rtn1=chk1.indexOf(ch1);
		if(rtn1==-1)
			return false;
	}
	return true;
}

function checkalnumdashdot(value,length)
{
	chk1="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._-@ ";
	for(i=0;i<length;i++)
	{
		ch1=value.charAt(i);
		rtn1=chk1.indexOf(ch1);
		if(rtn1==-1)
			return false;
	}
	return true;
}

function checkname(value,length)
{
	chk1="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ() .";
	for(i=0;i<length;i++)
	{
		ch1=value.charAt(i);
		rtn1=chk1.indexOf(ch1);
		if(rtn1==-1)
			return false;
	}
	return true;
} 

function textCounter(field, countfield, maxlimit)
{
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
		// otherwise, update 'characters left' counter
	else
		countfield.value = maxlimit - field.value.length;
}

function count_chars(field , countfield)
{
//	alert(field.value);
	countfield.value = field.value.length;
}


function usernameValidationatoz0to9(value,length)
{
	chk1="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	for(i=0;i<length;i++)
	{
		ch1=value.charAt(i);
		rtn1=chk1.indexOf(ch1);
		if(rtn1==-1)
			return false;
	}
	return true;
}

function GetXmlHttpObject(handler)
{ 
		var objXMLHttp=null
		if (window.XMLHttpRequest)
		{
			objXMLHttp=new XMLHttpRequest()
		}
		else if (window.ActiveXObject)
		{
			objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
		}
		return objXMLHttp
}

function loadpreview(pressid)
{
	xmlHttp = GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 
	var url="igallery-preview.php?prid="+pressid
	xmlHttp.onreadystatechange = Writepreview 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)		
}

function Writepreview()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		document.getElementById("imagepreview").innerHTML = xmlHttp.responseText 
	}						
}

function loadthumb(pressid)
{
	xmlHttp1 = GetXmlHttpObject()
	if (xmlHttp1 == null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 
	var url="igallery-thumbnail.php?prid="+pressid
	xmlHttp1.onreadystatechange = Writethumb 
	xmlHttp1.open("GET",url,true)
	xmlHttp1.send(null)		
}

function Writethumb()
{
	if (xmlHttp1.readyState==4 || xmlHttp1.readyState=="complete")
	{ 
		alert(xmlHttp1.responseText);
		document.getElementById("boxes").innerHTML = xmlHttp1.responseText 
	}						
}

function open_popup(url)
{
	//alert(url);
	var newWindow;
	var t;
	t = "no";
	if (String(t)=='no') {
		newWindow = window.open(url,"message","toolbar=" + t + ",scrollbars=yes,left=100,resizable=yes,width=500,height=600");
	}
	if (String(t)=='yes') {
		newWindow = window.open(url,"message","scrollbars=yes,resizable=yes,menubar=yes,status=yes,toolbar=yes,directories=yes,personalbar=yes,location=yes,hotkeys=yes,width=500,height=600");
	}
	if (window.focus) { newWindow.focus(); }

	//newwindow = window.open (url,'Hi','width=800,height=600,middle');
	//newwindow.focus();
	//var y = window.prompt("please enter user name") 
	//alert (y);
}

/* Remove Multiple whilte Spaces into single space */
function compressWhiteSpace(s) {

  // Condense white space.

  s = s.replace(/\s+/g, " ");
  s = s.replace(/^\s(.*)/, "$1");
  s = s.replace(/(.*)\s$/, "$1");

  // Remove uneccessary white space around operators, braces and parentices.

  s = s.replace(/\s([\x21\x25\x26\x28\x29\x2a\x2b\x2c\x2d\x2f\x3a\x3b\x3c\x3d\x3e\x3f\x5b\x5d\x5c\x7b\x7c\x7d\x7e])/g, "$1");
  s = s.replace(/([\x21\x25\x26\x28\x29\x2a\x2b\x2c\x2d\x2f\x3a\x3b\x3c\x3d\x3e\x3f\x5b\x5d\x5c\x7b\x7c\x7d\x7e])\s/g, "$1");
  return s;
}

function countWordsOfSentances(stringdata)
{
	stringdata = compressWhiteSpace(stringdata);;
	stringdataArray = stringdata.split(" ");	
	var words = stringdataArray.length;	
	return words;
}

/* For New 4 step process */

function openA(URL,WindowName)
{
	window.open(URL,WindowName, 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=460,height=450,screenX=20,screenY=40,left=20,top=10');
}

function textCounter1_second(field, countfield, minlimit)
{
	countfield.value =  field.value.length;
}

/* Add For PR Firm Profile - Staff Module */

function blank_div(targetdiv)
{
	document.getElementById(targetdiv).innerHTML='';
}

function hide_div(targetdiv)
{
	document.getElementById(targetdiv).innerHTML='';
	document.getElementById(targetdiv).style.display='none';
}

function isValidName(value,length)
{
	chk1="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
	for(i=0;i<length;i++)
	{
		ch1=value.charAt(i);
		rtn1=chk1.indexOf(ch1);
		if(rtn1==-1)
			return false;
	}
	return true;
}

/* Phone number validation  */

var validWorldPhoneChars = "+()- ";
var minDigitsInIPhoneNumber = 10;

function checkInternationalPhone(strPhone)
{
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
	alert(strPhone);
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}
function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

/* End of phone validation */

function nameValidationatoz0to9space(value,length)
{
	chk1="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
	for(i=0;i<length;i++)
	{
		ch1=value.charAt(i);
		rtn1=chk1.indexOf(ch1);
		if(rtn1==-1)
			return false;
	}
	return true;
}

/* showhints for field's hints */

function showhints(divid) 
{
  var divid1 = document.getElementById(divid);

  if (divid1.style.display != 'block') {
	  divid1.style.display = 'block';
  }
  else {
	  divid1.style.display = 'none';
  }
}

function wordClean(wordData)
{
 // Format data for cleanup
 wordData = wordData.replace(/[\n]+|\&nbsp\;| [ ]*/g, ' ');
 if ((wordData.indexOf('class=Mso')>=0) | (wordData.indexOf('class="Mso')>=0))
 {
  // kill unwanted tags
  wordData = wordData.replace(/<\?xml:[^>]*>/g,''); // Word xml
  wordData = wordData.replace(/<\/?st1:[^>]*>/g,'');  // Word SmartTags
  wordData = wordData.replace(/<\/?[a-z]\:[^>]*>/g,'');  // All other funny Word non-HTML stuff
  wordData = wordData.replace(/<\/?(span|div)[^>]*>/gi,'');
  wordData = wordData.replace(/<\/?font[^>]*>/gi,''); // Disable if you want to keep font formatting
  wordData = wordData.replace(/<!--[^>]*>/gi,''); // Word comments
  // preserve formatting
  wordData = wordData.replace(/(<P [^>]*(MARGIN: 0in 0in 0pt 0\.5*in|margin-left: 0\.5*in)[^>]*>.*?<\/p>)/gi,'<blockquote>$1</blockquote>'); // Indent
  wordData = wordData.replace(/<p [^>]*TEXT-ALIGN: ([a-z]*)[^>]*>/gi,'<p align=$1>'); // Alignment
  // keep tags, strip attributes
  wordData = wordData.replace(/ class=[^\s|>]*/gi,'');
  wordData = wordData.replace(/ style=\"[^>]*\"/gi,'');
  wordData = wordData.replace(/<(b|i|li|ul) [^>]*>/gi,"<$1>");
  // remove empty tags
  wordData = wordData.replace(/>[ ]*</g,'><');
  wordData = wordData.replace(/<p[^>]*><\/p>/gi,'\n'); // Preserve Formatting
  wordData = wordData.replace(/<span[^>]*><\/span>|<strong><\/strong>|<em><\/em>|<h[1-6]><\/h[1-6]>/gi,'');
  // preserve single spacing
  wordData = wordData.replace(/<\/blockquote><blockquote>/gi,''); // Extra blockquotes
  wordData = wordData.replace(/(<\/p><p[^>]*>)/gi,'<br>'); // Should be <br>, not <p>
 }

 wordData = wordData.replace(/<(\/?)b>/gi,'<$1strong>');
 wordData = wordData.replace(/<(\/?)i>/gi,'<$1em>');

 // format
 wordData = wordData.replace(/<\/(p|h[1-6]|table|tr|ol|ul|li|blockquote)>/gi,"<\/$1>");

 wordData = wordData.replace(/&nbsp;/gi,'spacessomdesaed');
 wordData = wordData.replace(/&amp;/gi,'andsomesaed');
 wordData = wordData.replace(/&gt;/gi,'greateromesaed');
 wordData = wordData.replace(/&lt;/gi,'loweromesaed');
 wordData = wordData.replace(/&ndash;/gi,'ndashdsd');
 wordData = wordData.replace(/&reg;/gi,'regasdd');
 wordData = wordData.replace(/&;/gi,'ampersnds');
 wordData = wordData.replace(/&ldquo;/gi,'dblqte');
 wordData = wordData.replace(/&quot;/gi,'');
 wordData = wordData.replace(/&rsquo;/gi,'snglqte');
 wordData = wordData.replace(/&rsquo;/gi,'snglqit');
 wordData = wordData.replace(/&rdquo;/gi,'dblqte');
 //wordData = wordData.replace(/ /gi,'spacessomdesaed');
 wordData = wordData.replace(/<br[ \/]*>/gi,"<br\/>");
 wordData = wordData.replace(/<br&nbsp;\/>/gi,"<br\/>");
 
 return wordData;
}

// For new features - URL Anchor

function anchorValidationatoz(value,length)
{
	chk1="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ- ";
	for(i=0;i<length;i++)
	{
		ch1=value.charAt(i);
		rtn1=chk1.indexOf(ch1);
		if(rtn1==-1)
			return false;
	}
	return true;
}

function checknewcompanyname(value,length)
{
	chk1="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._-, ";
	for(i=0;i<length;i++)
	{
		ch1=value.charAt(i);
		rtn1=chk1.indexOf(ch1);
		if(rtn1==-1)
			return false;
	}
	return true;
}

