// JavaScript Document
/* 
* Author: Rajan Maharjan
* Date : 8th June, 2009
* Reason : Implementing of jquery and stylish error display

*/

function validateFCKEditor(fckfield){
	
	// This functions shows that you can interact directly with the editor area
	// DOM. In this way you have the freedom to do anything you want with it.

	// Get the editor instance that we want to interact with.
	var oEditor = FCKeditorAPI.GetInstance(fckfield) ;

	// Get the Editor Area DOM (Document object).
	var oDOM = oEditor.EditorDocument ;

	var iLength ;

	// The are two diffent ways to get the text (without HTML markups).
	// It is browser specific.

	if ( document.all )		// If Internet Explorer.
	{
		iLength = oDOM.body.innerText.length ;
	}
	else					// If Gecko.
	{
		var r = oDOM.createRange() ;
		r.selectNodeContents( oDOM.body ) ;
		iLength = r.toString().length ;
	}
alert(iLength);
	
	
	if(iLength==0){
		alert("Please provide content for fckeditor field '" + fckfield +"'");
		}
	}

function checkPassword()
{
$pass=$("#password").val();
$repass=$("#cpassword").val();
	if($pass!=$repass)
		{
		$("#verifypassword").html('<font color="#FF0000">Error:: Passwords Mismatched.</font>');
		}
	else
		{
		$("#verifypassword").html('<font color="#009900">Ok:: Passwords Verified.</font>');
		}	
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function displayStyleErr(idTodisplay, errorMsg){	
	if(errorMsg!=''){		
		inlineMsg(idTodisplay,errorMsg,2);					
		$(idTodisplay).focus();
		$(idTodisplay).select();		
		return false;
	}
	else
		return true;
	}

function isNumber(myNumber){
	if(myNumber=='' || isNaN(myNumber))
		return false;
	else
		return true;
}

function isValidURL(theurl) {	
    var urlMatchOne= /(http|https):\/\/www\.[A-Za-z0-9\.-]{3,}\.[A-Za-z]{2,}/;	
	if(urlMatchOne.test(theurl)){     
         return true;     
		}
     else      
         return false;  
}


function isValidEmail(strEmail){	
	
var myregEmail=/^[_a-z0-9]{1,}(\.[a-z0-9]{1,})*@[a-z0-9]+(\.[a-z0-9-])*(\.[a-z]{2,})$/;
return myregEmail.test(strEmail);
}


function validate(field,form)
{
	
	try
	{
	valiclass=field.getAttribute("valiclass");	
	valimessage=field.getAttribute("valimessage");
	idOfField=field.getAttribute("id");
	
	
	if(valiclass=="required"){
	req=field.getAttribute("req");
	pattern="\\w{"+req+",}";
	if(!field.value.match(pattern)){	
		return displayStyleErr(idOfField,valimessage);	
		}
	}
	
	else if(valiclass=="txtarea" && field.value==''){		
		return displayStyleErr(idOfField,valimessage);	
		}	
	else if(valiclass=="urllink")
	{
		if(!isValidURL(field.value))
		{
		return displayStyleErr(idOfField,valimessage);	
		}
	}
	
	else if(valiclass=="email")
	{
		
		if(isValidEmail(field.value)==0)
		{
		return displayStyleErr(idOfField,valimessage);	
		}
	}
	else if(valiclass=="checkbox")
	{
		//!document.frmsignup.chkAccept.checked
		if(!field.checked)
		{
			return displayStyleErr(idOfField,valimessage);	
		}
	
	}
	else if(valiclass=="number")
	{
		
	if(isNumber(field.value)==false)
	{
	return displayStyleErr(idOfField,valimessage);	
	}
		
	}

	else if(valiclass=="PIN")
	{
	pattern="[0-9]{13}$";
	if((field.value.length!=13) || (!field.value.match(pattern)))
	{
	return displayStyleErr(idOfField,valimessage);	
	}
	
	}
	else if(valiclass=="zip")
	{
	pattern="[0-9]{5}$";
	//if(!field.value.match(pattern))
	if((field.value.length!=5) || (!field.value.match(pattern)))
	{
		return displayStyleErr(idOfField,valimessage);	
	}
	
	}
	
	else if(valiclass == "pass")
	{	
	if((field.value.length<5))
	{
	return displayStyleErr(idOfField,valimessage);	
	}
	}
	else if(valiclass == "cpass")
	{	
		
		if(form.password.value != form.cpassword.value)
			{
				return displayStyleErr(idOfField,valimessage);	
			}
	}
	else if(valiclass == "cemail")
	{	
		if(form.email.value != form.cemail.value)
			{
				return displayStyleErr(idOfField,valimessage);				
			}
	}
	
	
	else if(valiclass=="select")
	{
	if(field.value=="0")
	{
	return displayStyleErr(idOfField,valimessage);				
	}
	}
	else if(valiclass=='imageFile'){
		var req=field.getAttribute("req");		
		if($("#"+idOfField).val()=='' && req==1 && !isValidImage(idOfField)){			
			return displayStyleErr(idOfField,valimessage);	
			}
		else if($("#"+idOfField).val()!='' && !isValidImage(idOfField)){
			return displayStyleErr(idOfField,valimessage);	
			}
		}
return true;
}
catch(ex)
{
alert(ex.message);
return true;
}
}

function call_validate(form,from,to)
{

for(counter=from;counter<to;counter++)
{
bool=validate(form[counter],form);
if(!bool)
{
return false;
break;
}
}
form.submit();
}

function call_validate_ajax(form,from,to)
{
for(counter=from;counter<to;counter++)
{
bool=validate(form[counter],form);
if(!bool)
{
return false;
break;
}
}
//return true;
form.submit();
}

function isValidImage(elementID)
{
	//alert('image validation');
	var imgpath = $("#"+elementID).val();
	if(imgpath != "")
	{
		// code to get File Extension..
		var arr1 = new Array;
		arr1 = imgpath.split("\\");
		var len = arr1.length;
		var img1 = arr1[len-1];
		var filext = img1.substring(img1.lastIndexOf(".")+1);
		
		filext = filext.toLowerCase();
		
		// Checking Extension
		if(filext == "jpg" || filext == "gif" || filext == "png")
			return true;
		
		else{
			return false;
		}
	}
	return false;
}
