// JavaScript Document
function ltrim(s)
{
return s.replace(/^\s*/,"");
}
function rtrim(s)
{
return s.replace(/\s*$/,"");
}
function trim(s)
{
return rtrim(ltrim(s));
}
function hide_text(id,realvalue,boxvalue)
{
	if(boxvalue==realvalue)
		document.getElementById(id).value='';
}
function show_text(id,realvalue,boxvalue)
{
	if(trim(boxvalue)=='')
		document.getElementById(id).value=realvalue;
}
function Chk_Numeric(obj_nam,pos)    //Please Enter a Valid Product Price
 {
	 var x=document.getElementById(obj_nam).value; 
	 if(pos==1)//for first_name,last_name validation,city
	 {
		 var invalids = "`~@#$%^&*()_-+=\|{}[]:;'\"<>,.?/";
		 for(i=0; i<invalids.length; i++) 
		 {    
			if(x.indexOf(invalids.charAt(i)) >= 0 || x==false)
			{
				return false;
			}
		 }
	 }
	 if(pos==2) //for comments validation
	 {
	 	var invalids = "`~#%^&*()-+=\|{}[]:;'\"<>?/ ";
		 for(i=0; i<invalids.length; i++) 
		 {    
			if(x.indexOf(invalids.charAt(i)) >= 0 || x==false)
			{
				return false;
			}
		 }
	 }
	 if(pos==3)//for company name validation
	 {
	 	var invalids = "`~#$%^&*()_-+=\|{}[]:;'\"<>?/,";
		 for(i=0; i<invalids.length; i++) 
		 {    
			if(x.indexOf(invalids.charAt(i)) >= 0 || x==false)
			{
				return false;
			}
		 }
	 }
	 if(pos==4) //Address validation
	 {
	 	var invalids = "`~^*|<>?";
		for(i=0; i<invalids.length; i++) 
		 {    
			if(x.indexOf(invalids.charAt(i)) >= 0 || x==false)
			{
				return false;
			}
		 }
	 }
	if(pos==5) //integer validation
	 {
	 	var invalids = "+-";
		for(i=0; i<invalids.length; i++) 
		 {    
			if(x.indexOf(invalids.charAt(i)) >= 0 || x==false)
			{
				return false;
			}
		 }
	 }
	 return true;
 }
// Function to check wether email contains 2 chars after @ symbol and 2 chars after . symbol
function checkEmailFormat(email) {
	var myFirstArray = email.split('@');
	var mySecondArray = myFirstArray[1].split('.');
	if(mySecondArray[0].length >= 2 && mySecondArray[1].length >= 2)
	{
		return true;
	}
	return false;
}
//validation for Email format
function checkEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var reg1 = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-])+\.([A-Za-z]{2,4})+\.([A-Za-z]{2,4})+\.([A-Za-z]{2,4})$/;
   if(reg1.test(email) == true) {
      return false;
   }
   if(reg.test(email) == false) {
      return false;
   }
else
return checkEmailFormat(email);
}
function urlFormat(url)//function to check url of the form http://www.xxx.com
{
	//pattern for url of format http://www.xxx.com
	var p=/^(http|https)\:\/\//;
	if(p.test(url)==true)
		return true;
	else
		return false;
}
function imageFormat(image)
{
	var pat=/^[a-z0-9]([\\]|[\/]|[\_]|[\-]|[\@]|[\#]|[\$]|[\&]|[\*]|[\!]|[\~]|[\s]|[a-z0-9])+[\.]([jpg]|[jpeg]|[png]|[gif])/i;
	if(pat.test(image)==true)
		return true;
	else
		return false;
}
function checkUsername(uname) {
	var userreg = /^([A-Za-z0-9 \n])+$/;
	var flag = 0;
	if(userreg.test(uname) == false)
	{
	  flag = 1;//Contains Invalid characters
	}
	else
	{
		var fureg = /^([A-Za-z])+$/;
		var fchar;
		
		//Checks the first character of the username should be character
		/*
		fchar = uname.charAt(0);
		if(fureg.test(fchar) == false)
			flag = 2;//Contains Invalid first characters
		else
			flag = 3;//Contains Valid characters
		*/	
			
		//Check for atleast one character exists in username	
		for(i=0; i<uname.length; i++) 
		 { 
			fchar = uname.charAt(i);
			if(fureg.test(fchar) == true)
			{
				flag = 3;//Contains Valid characters
				break;
			}
		 }
	}
	return flag;
}
function logincheck()
{
mErrStr = "";
mFieldName = "";
	if(trim(document.loginform.username.value) == "")
	{
		mErrStr +="Username \n";
		if(mFieldName == "")
		mFieldName = "username";
	}
	else if(Chk_Numeric('username',2)==false)
	{
		mErrStr +="Valid Username \n";
		if(mFieldName == "")
		mFieldName = "username";
	}
	if(trim(document.loginform.password.value) == "")
	{ 
		mErrStr +="Password \n";
		if(mFieldName == "")
			mFieldName = "password";
	}
}
function Adddircategory()
{
 mErrStr = "";
 mFieldName = "";
	if(trim(document.category.categoryname.value) == "")
	{
	mErrStr +="Category Name \n";
	if(mFieldName == "")
		mFieldName = "categoryname";
	}
	if(document.category.catid.value=='')
	{
		if(trim(document.category.fileField.value) == "")
		{
			mErrStr +="Please Upload an Image. \n";
			if(mFieldName == "")
				mFieldName = "fileField";
		}		
	}
	if(document.category.fileField.value)
	{
		if (checkPhoto('fileField'))
		{
			mErrStr +="Image should be of the format .jpg,.jpeg,.png or .gif \n";
			mFieldName = "fileField";
		}
	}
	if(trim(document.category.description.value) == "")
	{	
		mErrStr +="Please enter Description ";
		if(mFieldName == "")
			mFieldName = "description";		
	}
}
function Addeditdircategory()
{
 mErrStr = "";
 mFieldName = "";
	if(trim(document.category.categoryname.value) == "")
	{
	mErrStr +="Category Name \n";
	if(mFieldName == "")
		mFieldName = "categoryname";
	}
	if(document.category.fileField.value)
	{
	var image=document.category.fileField.value;
// 		if (imageFormat(image)==false)
// 		{
// 			mErrStr +="Image should be of the format .jpg,.jpeg,.png or .gif \n";
// 			mFieldName = "fileField";
// 		}
	}
}
function Addaffcategory()
{
 mErrStr = "";
 mFieldName = "";
	if(trim(document.affiliate.programadd_cid.value) == "")
	{
	mErrStr +="Select a Category \n";
	if(mFieldName == "")
		mFieldName = "program_cid";
	}
	if(trim(document.affiliate.programadd_scid.value) == "")
	{
	mErrStr +="Select a Sub Category \n";
	if(mFieldName == "")
		mFieldName = "program_scid";
	}
	if(trim(document.affiliate.listingTitle.value) == "")
	{
	mErrStr +="Manage Program Title \n";
	if(mFieldName == "")
		mFieldName = "listingTitle";
	}
	if(document.affiliate.programid.value=='')
	{
		if(trim(document.affiliate.fileField.value) == "")
		{
			mErrStr +="Please Upload a Screenshot \n";
			if(mFieldName == "")
				mFieldName = "fileField";
		}
	}
	if(document.affiliate.fileField.value)
	{
	var image=document.affiliate.fileField.value;
		if (checkPhoto('fileField'))
		{
			mErrStr +="Image should be of the format .jpg,.jpeg,.png or .gif \n";
			mFieldName = "fileField";
		}
	}
	if(trim(document.affiliate.listingDescription.value) == "")
	{
	mErrStr +="Description \n";
	if(mFieldName == "")
		mFieldName = "listingDescription";
	}
	if(trim(document.affiliate.useremail.value) == "")
	{
	mErrStr +="Affiliate Manager E-mail \n";
	if(mFieldName == "")
		mFieldName = "useremail";
	}
	if(document.affiliate.useremail.value)
	{
		var emailID=document.affiliate.useremail;
		if (checkEmail(emailID.value)==false)
		{
			mErrStr +="Valid Email Address \n";
			mFieldName = "useremail";
		}
	} 
	if(trim(document.affiliate.userurl.value) == "")
	{
	mErrStr +="Site URL \n";
	if(mFieldName == "")
		mFieldName = "userurl";
	}
	if(document.affiliate.userurl.value)
	{
		var url=document.affiliate.userurl;
		if (urlFormat(url.value)==false)
		{
			mErrStr +="Please enter Valid URL \n";
			mFieldName = "userurl";
		}
	}
	if(trim(document.affiliate.userbase.value) == "")
	{
	mErrStr +="Base Commission \n";
	if(mFieldName == "")
		mFieldName = "userbase";
	}
	if(trim(document.affiliate.userbasetype.value) == "")
	{
	mErrStr +="Commission Type \n";
	if(mFieldName == "")
		mFieldName = "userbasetype";
	}
	if(document.affiliate.multi.length>1)
	{
	if(document.affiliate.multi[0].checked!=true && document.affiliate.multi[1].checked!=true)
	{
	mErrStr +="Multi-tier \n";
	if(mFieldName == "")
		mFieldName = "multi";
	}
	}
	if(trim(document.affiliate.userpay.value) == "")
	{
	mErrStr +="Payment Frequency \n";
	if(mFieldName == "")
		mFieldName = "userpay";
	}
	if(trim(document.affiliate.userpaytype.value) == "")
	{
	mErrStr +="Payment Type \n";
	if(mFieldName == "")
		mFieldName = "userpaytype";
	}
	if(document.affiliate.programid.value=='')
	{
		if(trim(document.affiliate.fileField2.value) == "")
		{
			mErrStr +="Please Upload a Logo \n";
			if(mFieldName == "")
				mFieldName = "fileField2";
		}
	}
	if(document.affiliate.fileField2.value)
	{
	var image=document.affiliate.fileField2.value;
		if (checkPhoto('fileField2'))
		{
			mErrStr +="Image should be of the format .jpg,.jpeg,.png or .gif \n";
			mFieldName = "fileField2";
		}
	}
	if(document.affiliate.programid.value=='')
	{
		if(trim(document.affiliate.fileField3.value) == "")
		{
			mErrStr +="Please Upload a Home Page Logo \n";
			if(mFieldName == "")
				mFieldName = "fileField3";
		}
	}
	if(document.affiliate.fileField3.value)
	{
	var image=document.affiliate.fileField3.value;
		if (checkPhoto('fileField3'))
		{
			mErrStr +="Image should be of the format .jpg,.jpeg,.png or .gif \n";
			mFieldName = "fileField3";
		}
	}
	if(trim(document.affiliate.userlogourl.value) == "")
	{
	mErrStr +="Logo URL \n";
	if(mFieldName == "")
		mFieldName = "userlogourl";
	}
	if(document.affiliate.userlogourl.value)
	{
		var url=document.affiliate.userlogourl;
		if (urlFormat(url.value)==false)
		{
			mErrStr +="Please enter Valid URL \n";
			mFieldName = "userlogourl";
		}
	}
}
function Addaffeditcategory()
{
 mErrStr = "";
 mFieldName = "";
	if(trim(document.affiliate.programedit_cid.value) == "")
	{
	mErrStr +="Select a Category \n";
	if(mFieldName == "")
		mFieldName = "program_cid";
	}
	if(trim(document.affiliate.programedit_scid.value) == "")
	{
	mErrStr +="Select a Sub Category \n";
	if(mFieldName == "")
		mFieldName = "program_scid";
	}
	if(trim(document.affiliate.listingTitle.value) == "")
	{
	mErrStr +="Manage Program Title \n";
	if(mFieldName == "")
		mFieldName = "listingTitle";
	}
	if(document.affiliate.fileField.value)
	{
	var image=document.affiliate.fileField.value;
		if (imageFormat(image)==false)
		{
			mErrStr +="Image should be of the format .jpg,.jpeg,.png or .gif \n";
			mFieldName = "fileField";
		}
	}
	if(trim(document.affiliate.listingDescription.value) == "")
	{
	mErrStr +="Description \n";
	if(mFieldName == "")
		mFieldName = "listingDescription";
	}
	if(trim(document.affiliate.useremail.value) == "")
	{
	mErrStr +="Affiliate Manager E-mail \n";
	if(mFieldName == "")
		mFieldName = "useremail";
	}
	if(document.affiliate.useremail.value)
	{
		var emailID=document.affiliate.useremail;
		if (checkEmail(emailID.value)==false)
		{
			mErrStr +="Valid Email Address \n";
			mFieldName = "useremail";
		}
	} 
	if(trim(document.affiliate.userurl.value) == "")
	{
	mErrStr +="Site URL \n";
	if(mFieldName == "")
		mFieldName = "userurl";
	}
	if(document.affiliate.userurl.value)
	{
		var url=document.affiliate.userurl;
		if (urlFormat(url.value)==false)
		{
			mErrStr +="Please enter Valid URL \n";
			mFieldName = "userurl";
		}
	}
	if(trim(document.affiliate.userbase.value) == "")
	{
	mErrStr +="Base Commission \n";
	if(mFieldName == "")
		mFieldName = "userbase";
	}
	if(trim(document.affiliate.userbasetype.value) == "")
	{
	mErrStr +="Commission Type \n";
	if(mFieldName == "")
		mFieldName = "userbasetype";
	}
	if(document.affiliate.multi.length>1)
	{
	if(document.affiliate.multi[0].checked!=true && document.affiliate.multi[1].checked!=true)
	{
	mErrStr +="Multi-tier \n";
	if(mFieldName == "")
		mFieldName = "multi";
	}
	}
	if(trim(document.affiliate.userpay.value) == "")
	{
	mErrStr +="Payment Frequency \n";
	if(mFieldName == "")
		mFieldName = "userpay";
	}
	if(trim(document.affiliate.userpaytype.value) == "")
	{
	mErrStr +="Payment Type \n";
	if(mFieldName == "")
		mFieldName = "userpaytype";
	}
	if(document.affiliate.fileField2.value)
	{
	var image=document.affiliate.fileField2.value;
		if (imageFormat(image)==false)
		{
			mErrStr +="Image should be of the format .jpg,.jpeg,.png or .gif \n";
			mFieldName = "fileField";
		}
	}
	if(trim(document.affiliate.userlogourl.value) == "")
	{
	mErrStr +="Logo URL \n";
	if(mFieldName == "")
		mFieldName = "userlogourl";
	}
	if(document.affiliate.userlogourl.value)
	{
		var url=document.affiliate.userlogourl;
		if (urlFormat(url.value)==false)
		{
			mErrStr +="Please enter Valid URL \n";
			mFieldName = "userlogourl";
		}
	}
}
function Addsubcategory()
{
mErrStr = "";
 mFieldName = "";
	if(trim(document.subcategory.sub_cat_name.value) == "")
	{
	mErrStr +="Category Name \n";
	if(mFieldName == "")
		mFieldName = "sub_cat_name";
	}
	if(trim(document.subcategory.sub_category_name.value) == "")
	{
	mErrStr +="Sub-Category Name \n";
		if(mFieldName == "")
		mFieldName = "sub_category_name";
	}
	
}
function Addcategory()
{
 mErrStr = "";
 mFieldName = "";
	if(trim(document.articlecategory.category_name.value) == "")
	{
	mErrStr +="Category Name \n";
	if(mFieldName == "")
		mFieldName = "category_name";
	}
}
function Addarticle()
{
mErrStr = "";
 mFieldName = "";
	if(trim(document.articles.category.value) == "")
	{
	mErrStr +="Category Name \n";
	if(mFieldName == "")
		mFieldName = "category";
	}
	if(trim(document.articles.article_title.value) == "")
	{
	mErrStr +="Title \n";
		if(mFieldName == "")
		mFieldName = "article_title";
	}
	if(trim(document.articles.description.value) == "")
	{
	mErrStr +="Description \n";
	if(mFieldName == "")
		mFieldName = "description";
	}
}
function checkPhoto(picField) {
 var picFile = picField;
 var imagePath = document.getElementById(picField).value;
 var pathLength = imagePath.length;
 var lastDot = imagePath.lastIndexOf(".");
 var fileType = imagePath.substring(lastDot,pathLength);
 if((fileType == ".gif") || (fileType == ".jpg") || (fileType == ".png") || (fileType == ".GIF") || (fileType == ".JPG") || (fileType == ".PNG")) {
  
 } else {
//   alert("Please upload .JPG, .PNG, and .GIF image formats. Your file-type is " + fileType + ".");
// document.getElementById(picField).focus();
return true;
 }

}
function validCreditCard()
{
/* Developed by Abdirahman Sh. Abdisalam @ www.clik.to/program */
	cardNumber= document.getElementById('cardnumber').value;
	if(cardNumber=="")
	{
		alert("Please Enter your Credit  or Debit card number");
		document.getElementById('cardnumber').focus();
		document.getElementById('cnumber').select();
		return false;
	}
	for(x=0;x<cardNumber.length;x++)
	{
		if(cardNumber.charCodeAt(x)<48 || cardNumber.charCodeAt(x)>57)
		{
			alert("Credit or debit card number should contain numbers only");
			document.getElementById('cardnumber').focus();
		document.getElementById('cardnumber').select();
			return false;
		}
	}
	if(document.getElementById('cardtype').value=='V')
	{
		cardType="V";
	}
	else if(document.getElementById('cardtype').value=='M')	
	{
		cardType="M";
	}
	else if(document.getElementById('cardtype').value=='D')	
	{
		cardType="D";
	}
	else if(document.getElementById('cardtype').value=='A')	
	{
		cardType="A";
	}
	
	
	switch(cardType)
	{
	
	case "M":
		if(cardNumber.length!=16 || cardNumber.substr(0,2) <51 || cardNumber.substr(0,2)>55 )
		{
			alert("Invalid Master card number");
			document.getElementById('cardnumber').focus();
			document.getElementById('cardnumber').select();
			return false;
		}
		else
		{
			return true;
		}
		break;
	case "V":
		if(cardNumber.substr(0,1) !=4)
		{
			alert("Invalid Visa number");	
			document.getElementById('cardnumber').focus();
			document.getElementById('cardnumber').select();
			return false;
		}
		else if(cardNumber.length!=16 && cardNumber.length!=13)
		{
			alert("Invalid Visa number");
			document.getElementById('cardnumber').focus();
			document.getElementById('cardnumber').select();
			return false;
		}
		else
		{
			return true;
		}
		break;
	case "D":
		if(cardNumber.length!=16)
		{
			alert("Invalid Discover number");
			document.getElementById('cardnumber').focus();
			document.getElementById('cardnumber').select();
			return false;
		}
		else
		{
			return true;
		}
		break;
	case "A":
		if(cardNumber.length !=15)
		{
			alert("Invalid American Express number");
			document.getElementById('cardnumber').focus();
			document.getElementById('cardnumber').select();
			return false;
		}
		else if(cardNumber.substr(0,2)!=37 && cardNumber.substr(0,2)!=34)
		{
			alert("Invalid American Express number");
			document.getElementById('cardnumber').focus();
			document.getElementById('cardnumber').select();
			return false;
		}
		else
		{
			return true;
		}
		break;
	
	default:
	alert("Card type unceptable");
}
	return true;
}
