// trim white space

function trim(strText) {
    // this will get rid of leading spaces
    while (strText.substring(0,1) == ' ')
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
}


function stripSpace(x){
	var emptySpace = / /g;
	var trimAfter = x.replace(emptySpace,"");
	return(trimAfter);
}

function textValidate(incomingString, defaultValue){
	if(stripSpace(incomingString).length == 0 || incomingString.search(/[^a-zA-Z ]/g) != -1 || incomingString==defaultValue || incomingString.charAt(0)==" "){
		return false;
	} else {
		return true;
	}
}

function labkeyValidate(incomingString, defaultValue){
	if(stripSpace(incomingString).length == 0 || incomingString.search(/[^a-zA-Z \,]/g) != -1 || incomingString==defaultValue){
		return false;
	} else {
		return true;
	}
}

function genValidate(incomingString, defaultValue) {
	if(stripSpace(incomingString).length == 0 || incomingString==defaultValue) {
		return false;
	} else {
		return true;
	}
}

function isAlphaNumeric(incomingString, defaultValue) {
	if(stripSpace(incomingString).length == 0 || incomingString.search(/[^0-9a-zA-Z ]/g) != -1 || incomingString==defaultValue) {
		return false;
	} else {
		return true;
	}
}
function AlphaNumericNoSpace(incomingString, defaultValue) {
	if(stripSpace(incomingString).length == 0 || incomingString.search(/[^0-9a-zA-Z]/) != -1 || incomingString==defaultValue) {
		return false;
	} else {
		return true;
	}
}

function numberValidate(incomingString, defaultValue) {
	if(stripSpace(incomingString).length == 0 || incomingString.search(/[^0-9]/g) != -1 || incomingString==defaultValue || parseInt(incomingString, 10) <= 0 ) {
		return false;
	} else {
		return true;
	}
}
function decimalValidate(incomingString, defaultValue) {
	if(stripSpace(incomingString).length == 0 || incomingString.search(/[^0-9\.]/g) != -1 || incomingString==defaultValue || parseInt(incomingString, 10) <= 0 ) {
		return false;
	} else {
		return true;
	}
}

function dateValidate(incomingString, defaultValue) {
	if(stripSpace(incomingString).length == 0 || incomingString.search(/[^0-9\-\\\/]/g) != -1 || incomingString==defaultValue || parseInt(incomingString, 10) <= 0 ) {
		return false;
	} else {
		return true;
	}
}

function dateRangeValidate() {
	today = new Date();
	var today_date = parseInt(today.getDate());
	var today_month = parseInt(today.getMonth() + 1);
	var today_year = parseInt(today.getFullYear());

	comparestring = document.getElementById('anpf_phtdte').value;
	var comp_year = parseInt(comparestring.substring (0, comparestring.indexOf ("-")));
	var comp_month = parseInt(comparestring.substring (comparestring.indexOf ("-") + 1, comparestring.lastIndexOf ("-")));
	var comp_date = parseInt(comparestring.substring (comparestring.lastIndexOf ("-") + 1, comparestring.length));



	if (comp_year == today_year) {
		if (comp_month == today_month) {
			if (comp_date <= today_date) {
				//alert('Hurray');
				return 0;
			} else {
				//alert('date');
				return 1;
			}
		} else if (comp_month < today_month){
			//alert('Hurray');
			return 0;
		} else {
			//alert('month');
			return 1;
		}
	} else if (comp_year < today_year){
			//alert('Hurray');
			return 0;
		} else {
			//alert('year');
			return 1;
		}
}

function minLength(strIn, intLength) {
 return (String(strIn).length >= intLength);
}

function maxLength(strIn, intLength) {
 return (String(strIn).length <= intLength);
}
/*
function isAlphaNumeric(val)
{
	if (val.match(/^[a-zA-Z0-9\s]+$/))
	{
		return true;
	}
	else
	{
		return false;
	}
}
*/


function alphanumeric(i, d)
{
	if(stripSpace(i).length == 0 || i.search(/[^0-9a-zA-Z_\-\.\ \,\&]/g) != -1 || i==d)
		return false;
	else
		return true;
}

function addressvalidate(i, d)
{
	if(stripSpace(i).length == 0 || i.search(/[^0-9a-zA-Z_\-\.\ \,\&]\r/g) != -1 || i==d)
		return false;
	else
		return true;
}



/*Email Validation function*/
function emailValidate(i, d)
{
	if(trim(i).length == 0 || i.search(/^([a-zA-Z0-9_\-\.]+)@([a-zA-Z\-]+)\.(([a-zA-Z]{2,3}|[0-9]{1,3})|([a-zA-Z]{2}\.[a-zA-Z]{2}))$/) == -1 || i==d)
		return false;
	else
		return true;
}

function login_validate()
{
var emailexp =/^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9\-]+)\.(([a-zA-Z]{2,}|[0-9]{2,})|([a-zA-Z]{2,}\.[a-zA-Z]{2,})|([a-zA-Z]{2,}\.[a-zA-Z]{2,}\.[a-zA-Z]{2,}))$/;

		//if(!alphanumeric(document.getElementById('musername').value,''))
		 if(!emailexp.test(document.getElementById('musername').value))
			{
				alert("Please provide valid email");
				document.getElementById('musername').focus();
				document.getElementById('musername').select();
				return false;
			}
				// password check
		if(!isAlphaNumeric(document.getElementById('mpassword').value,''))
		{
			alert("Please provide password (AlphaNumeric only)");
			document.getElementById('mpassword').focus();
			document.getElementById('mpassword').select();
			return false;
		}
		document.getElementById('memberlogin').action="checklogin.php";
		document.getElementById('memberlogin').submit();

}
function member_login_validate()
{
	var emailexp =/^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9\-]+)\.(([a-zA-Z]{2,}|[0-9]{2,})|([a-zA-Z]{2,}\.[a-zA-Z]{2,})|([a-zA-Z]{2,}\.[a-zA-Z]{2,}\.[a-zA-Z]{2,}))$/;

		//if(!alphanumeric(document.getElementById('musername').value,''))
		 if(!emailexp.test(document.getElementById('musername').value))
			{
				alert("Please provide valid email");
				document.getElementById('musername').focus();
				document.getElementById('musername').select();
				return false;
			}
				// password check
		if(!isAlphaNumeric(document.getElementById('mpassword').value,''))
		{
			alert("Please provide password (AlphaNumeric only)");
			document.getElementById('mpassword').focus();
			document.getElementById('mpassword').select();
			return false;
		}
		document.getElementById('memberlogin').action="../checklogin.php";
		document.getElementById('memberlogin').submit();

}
//text counter
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;

}
//text counter Result
function textCounterResult(field, countfield, maxlimit) {


	if (field.length > maxlimit) // if too long...trim it!
	{
			field = field.substring(0, maxlimit);
			// otherwise, update 'characters left' counter

	}
	else
		document.getElementById('remLen').value = maxlimit - field.length;

}

// hide display status
function resulthide()
{

	document.getElementById("resultstatus").style.display="none";
}

//search validate
function search_homevalid()
{
		if((!alphanumeric(document.getElementById('searchkey').value,''))||document.getElementById('searchkey').value=="SEARCH")
			{
				alert("Please provide search word");
				document.getElementById('searchkey').focus();
				document.getElementById('searchkey').select();
				return false;
			}

		document.getElementById('frmsearch').action="search_result.php";
		document.getElementById('frmsearch').submit();

}

function search_membervalid()
{

		if((!alphanumeric(document.getElementById('searchkey').value,''))||document.getElementById('searchkey').value=="SEARCH" )
			{
				alert("Please provide search word");
				document.getElementById('searchkey').focus();
				document.getElementById('searchkey').select();
				return false;
			}

		document.getElementById('frmsearch').action="../search_result.php";
		document.getElementById('frmsearch').submit();

}
function onclear(){
	document.getElementById('searchkey').value="";
}

function clrsearch(){
	if(document.getElementById('searchkey').value == 'Search for Photographs'){
		document.getElementById('searchkey').value="";
	}
	else if(document.getElementById('searchkey').value == ''){
		document.getElementById('searchkey').value = "Search for Photographs";
		document.getElementById('searchkey').className = 'blurred';
	}
}
function searchstyle(){
	document.getElementById('searchkey').className = 'focussed';
}


// International Phonenumber validation

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

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;
}

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 checkInternationalPhone(strPhone){

s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function ValidateForm(){
	var Phone=document.frmSample.txtPhone

	if ((Phone.value==null)||(Phone.value=="")){
		alert("Please Enter your Phone Number")
		Phone.focus()
		return false
	}
	if (checkInternationalPhone(Phone.value)==false){
		alert("Please Enter a Valid Phone Number")
		Phone.value=""
		Phone.focus()
		return false
	}
	return true
 }

// Get Image width and height
function getImgSize(imgSrc, flag)
{

	if(!flag)
	{
		var newImg = new Image();
		imgSrc="file://"+imgSrc;
		newImg.src =imgSrc;
	}
	else
	var newImg = imgSrc;

if (typeof newImg.complete=='boolean'&&(newImg.complete&&newImg.height*1>0))
	{
		var height = newImg.height;
		var width = newImg.width;
		//alert ('The image size is '+width+'*'+height);
		alert("Image Height = "+height+"   Image width = "+width);
	}
	else if (typeof newImg.complete=='boolean')
	{
		setTimeout(function(){getImgSize(newImg,'x')}, 60);
	}
	else alert('Image cannot be evaluated for size');
}


// Other than member page singout
function onsignout(randid)
{
	window.location.href="index.php?sg="+randid;
}

function follow_user(usr,follow)
{
	//alert("hi");
		var usrname=usr;
		var followusr=follow;

			var strURL ="follow_user.php?action=followuser&usr_name="+usrname+"&follow_usr="+followusr+"";

			//alert(strURL);

			var xmlHttp;
			if(window.XMLHttpRequest)
			{
				var xmlHttp = new XMLHttpRequest();
			}
			else if(window.ActiveXObject)
			{
				var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			xmlHttp.open('POST', strURL, true);
			xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			xmlHttp.onreadystatechange = function()
			{
				if (xmlHttp.readyState == 4)
				{
					reply_msg(xmlHttp.responseText);
				}
			}
			xmlHttp.send(strURL);


}
function reply_msg(str)
{

	if(str==1)
	{
		alert("You're now following the photographer.");
		location.reload(true);

		}
	else if(str==0)
	{
		alert("You have already added your follower list");;
	}
	else
	{
		alert("Your follow user was not send");
	}
}

function unfollow_user(usr,follow)
{
	//alert("hi");
		var usrname=usr;
		var followusr=follow;

			var strURL ="follow_user.php?action=unfollowuser&usr_name="+usrname+"&follow_usr="+followusr+"";

			//alert(strURL);

			var xmlHttp;
			if(window.XMLHttpRequest)
			{
				var xmlHttp = new XMLHttpRequest();
			}
			else if(window.ActiveXObject)
			{
				var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			xmlHttp.open('POST', strURL, true);
			xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			xmlHttp.onreadystatechange = function()
			{
				if (xmlHttp.readyState == 4)
				{
					remove_msg(xmlHttp.responseText);
				}
			}
			xmlHttp.send(strURL);


}
function remove_msg(str)
{

	if(str==1)
	{
		alert("You are no more following the photographer.");
		location.reload(true);

		}
	else if(str==0)
	{
		alert("You have already stopped following the photographer.");;
	}
	else
	{
		alert("Unfollowing coudln't be activated. Please try again after sometime.");
	}
}

function reg_follow_user()
{
 	alert("Please login to follow the photographer.");
}