//------------------------------------------------------------------------------*/
//                            Copyright Notice							        */
//                            ~~~~~~~~~~~~~~~~							        */
//		Copyright (C) 2003  By Yongchuang Electronic Commerce Co.,Ltd.			*/
//																				*/
//		All rights reserved. This material is confidential and	proprietary to	*/
//	Yongchuang Electronic Commerce Co.,Ltd. and no part	of this material should	*/
//	be reproduced,published in any form	by any means,electronic or mechanical	*/
//	including photocopy	or any information storage or retrieval system nor		*/
//	should the material be disclosed to third parties without the express		*/
//	written authorization of Yongchuang Electronic Commerce Co.,Ltd.			*/
//																				*/
//------------------------------------------------------------------------------*/

//================================================================
//
//	File Name	:	Function.js
//
//	File Type	:	javascript source file
//
//	Author		:	Andy Wang
//	E-mail		:	King_wda@163.net
//
//	Purpose		:	common javascript function
//
//	Date		:	05/20/2003
//
//	Modification:	05/20/2003
//
//================================================================


var Numbers="0123456789";
var UpperLetters="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var LowerLetters="abcdefghijklmnopqrstuvwxyz";
var SpecialChar="`~!@#$%^&*()_-+={}[]|\:;<>,.?/'";
var EmailChar= Numbers + UpperLetters + LowerLetters + "@-_.";
var EnglishChar= Numbers + UpperLetters + LowerLetters + "`~!@#$%^&()_-+={}[];,.'";

var Letters=UpperLetters + LowerLetters ;
var Alphanumeric=Numbers + Letters ;



function isNumeric(theString){
//--------------------------------------------------------------------------------
//  Author 		:       Andy Wang
//  Version     Date			Description
//  1.0         05/21/2003   	Validate whether the string is numeric
//  Parameters		:
//  Name				Mode		Description
//  theNumber			Input		the validated string
//
//  Return Value        :   true/false
//  Return Type         :   boolean
//--------------------------------------------------------------------------------
	var num=theString;
	num=num*1 + "";
	if(num=="NaN")
		return false;
	else
		return true;
}

function isPositive(theString){
//--------------------------------------------------------------------------------
//  Author 		:       Andy Wang
//  Version     Date			Description
//  1.0         05/21/2003   	Validate whether the string is positive number
//  Parameters		:
//  Name				Mode		Description
//  theNumber			Input		the validated string
//
//  Return Value        :   true/false
//  Return Type         :   boolean
//--------------------------------------------------------------------------------
	if (isNumeric(theString)) {
		if (theString>=0){
			return true;
		}else{
			return false;
		}
	}else{
		return false;
	}
}

function isPositiveInteger(theString){	
//--------------------------------------------------------------------------------
//  Author 		:       Andy Wang
//  Version     Date			Description
//  1.0         05/21/2003   	Validate whether the string is positive integer
//  Parameters		:
//  Name				Mode		Description
//  theNumber			Input		the validated string
//
//  Return Value        :   true/false
//  Return Type         :   boolean
//--------------------------------------------------------------------------------
	//Is numeric?
	if (isNumeric(theString)) {
		var num=theString*1
		// >0 ?
		if (num>=0){
			num=num+"";
			//Include pointer "." ?
			if (num.indexOf(".")==-1){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}else{
		return false;
	}
}

function isDate(theString){
//--------------------------------------------------------------------------------
//  Author 		:       Andy Wang
//  Version     Date			Description
//  1.0         05/21/2003   	Validate whether the string is date
//  Parameters		:
//  Name				Mode		Description
//	theNumber			Input		the validated string
//
//  Return Value        :   true/false
//	Return Type         :   boolean
//--------------------------------------------------------------------------------
var theDate;
theDate=Date.parse(theString)+"";
if (theDate=="NaN" )
	return false;	
else
	return true;
}


function checkPassword(Password,HaveUpper,HaveLower,HaveSpecial,MinLen,MaxLen){	
//--------------------------------------------------------------------------------
//  Author 		:       Andy Wang
//  Version     Date			Description
//  1.0         05/21/2003   	Validate format of the password 
//  Parameters		:
//  Name				Mode		Description
//	Password			Input		the validated password
//	HaveUpper			Input		indicate password must have uppercase letter
//	HaveLower			Input		indicate password must have lowercase letter
//	HaveSpecial			Input		indicate password must have special letter
//	MinLen				Input		indicate minimum length of password 
//	MaxLen				Input		indicate maximal length of password 
//
//  Return Value        :   true/false
//  Return Type         :   boolean
//--------------------------------------------------------------------------------
	var i;
	var chr;
	var blnFound;
	var len;
	
	len=Password.length;
	
	//length must be between MinLen and MaxLen
	if (len<MinLen ||len> MaxLen){
		return false;
	}
	
	//Must have uppercase letters	
	if (HaveUpper==true){
		blnFound=false;
		for(i=0;i<len;i++){
			chr=Password.charAt(i);
			if (UpperLetters.indexOf(chr)!=-1){			//Password include uppercaes letter
				blnFound=true;
				break;
			}
		}
		if (!blnFound){
			return false;
		}
	}
	
	//Must have lowercase letters	
	if (HaveLower==true){
		blnFound=false;
		for(i=0;i<len;i++){
			chr=Password.charAt(i);
			if (LowerLetters.indexOf(chr)!=-1){			//Password include uppercaes letter
				blnFound=true;
				break;
			}
		}
		if (!blnFound){
			return false;
		}
	}
	
	//Must have special letters	
	if (HaveSpecial==true){
		blnFound=false;
		for(i=0;i<len;i++){
			chr=Password.charAt(i);
			if (SpecialChar.indexOf(chr)!=-1){			//Password include uppercaes letter
				blnFound=true;
				break;
			}
		}
		if (!blnFound){
			return false;
		}
	}	
	//ok.
	return true;
}

function setTextFocus(formKey,theText){	
//--------------------------------------------------------------------------------
//  Author 		:       Andy Wang
//  Version     Date			Description
//  1.0         05/21/2003   	Validate whether the string is date
//  Parameters		:
//  Name				Mode		Description
//	formKey				Input		the name or index of the form
//	theText				Input		the name of the selected control
//
//  Return Value        :   None
//	Return Type         :   None
//--------------------------------------------------------------------------------		
	document.forms[formKey].elements[theText].select();
	document.forms[formKey].elements[theText].focus();	
}


function SetOnlyRead(formKey,control){
//--------------------------------------------------------------------------------
//  Author 		:       Andy Wang
//  Version     Date			Description
//  1.0         05/21/2003   	Set control readonly
//  Parameters		:
//  Name				Mode		Description
//	formKey				Input		the name or index of the form
//	control				Input		the name of the control
//
//  Return Value        :   None
//	Return Type         :   None
//--------------------------------------------------------------------------------	
	var theContainer;
	theContainer=window.navigator.appName ;
	if(theContainer.indexOf("Microsoft")==-1){
		document.forms[formKey].elements[control].readOnly=true;
		document.forms[formKey].elements[control].style.backgroundColor="#E3E3E3";
	}else{		//Microsoft Internet Explorer	
		document.forms(formKey).elements(control).style.backgroundColor="#E3E3E3";	
		document.forms(formKey).elements(control).readOnly=true;
	}
}

function checkEmail(EmailAddress){
//--------------------------------------------------------------------------------
//  Author 		:       Andy Wang
//  Version     Date			Description
//  1.0         05/21/2003   	Validate whether the string is date
//  Parameters		:
//  Name				Mode		Description
//	EmailAddress		Input		the email address
//
//  Return Value        :   true/false
//	Return Type         :   boolean
//--------------------------------------------------------------------------------	
	var i=0;
	var chr;
	var len;
	
	len=EmailAddress.length;
	
	//validate all characters
	for(i=0;i<len;i++){
		chr=EmailAddress.charAt(i);
		if(EmailChar.indexOf(chr)==-1){			
			return false;
		}
	}
	
	//Email address must include "@"
	if(EmailAddress.indexOf("@")==-1) return false;		
	//Email address must include "."
	if(EmailAddress.indexOf(".")==-1) return false;	
	//Email address must include only one "@"
	if(EmailAddress.lastIndexOf("@")!=EmailAddress.indexOf("@")) return false;
	//"@" must not be the first or last character
	if(EmailAddress.indexOf("@")==0 || EmailAddress.lastIndexOf("@")==len-1 ) return false;
	//"." must be not the first or last character
	if(EmailAddress.indexOf(".")==0 || EmailAddress.lastIndexOf(".")==len-1 ) return false;
	
	//OK
	return true;
}

/*
function refreshOpener(){	
//--------------------------------------------------------------------------------
//  Author 		:       Andy Wang
//  Version     Date			Description
//  1.0         05/21/2003   	Refresh the opener of the current window
//  Parameters		:
//  None
//
//  Return Value        :   None
//	Return Type         :   None
//--------------------------------------------------------------------------------	
	window.opener.location=window.opener.location;	
}
*/


function mustNotBlank(formKey,elementName,minLen,maxLen,PromptString){
//--------------------------------------------------------------------------------
//  Author 		:       Andy Wang
//  Version     Date			Description
//  1.0         07/18/2003   	the form element must no be blank
//  Parameters		:
//	formKey				Input		the name or index of the form
//	elementName			Input		the name or element
//	minLen				Input		the min length
//	maxLen				Input		the max length, -1 : infinite
//	PromptString		Input		the prompted string
//
//  Return Value        :   None
//	Return Type         :   None
//	Functions Called    :	setTextFocus
//
//--------------------------------------------------------------------------------	
	var element=document.forms[formKey].elements[elementName];
	var theValue=element.value;
	var len=theValue.length;
	var i;
	var beBlank=true;
	//- must not be blank	
	for(i=0;i<len;i++){
		if(theValue.charAt(i)!=" "){
			beBlank=false;
			break;
		}
	}
	if(beBlank==true){
		alert(PromptString);
		setTextFocus(formKey,elementName);
		return false;
	}	
	
	//- minimal length
	if(len<minLen){
		alert(PromptString);
		setTextFocus(formKey,elementName);
		return false;	
	}
	//- maximal length ,-1:infinite
	if(maxLen!=-1){
		if(len>maxLen){
			alert(PromptString);
			setTextFocus(formKey,elementName);
			return false;
		}
	}
	return true;
}



function SaveSelectedIDS(formKey,checkBoxName,storeText){
//--------------------------------------------------------------------------------
//  Author 		:       Andy Wang
//  Version     Date			Description
//  1.0         07/14/2003   	将选择项的编号保存在文本框storeText中,以“,”分隔
//  Parameters		:
//  Name				Mode		Description
//	formKey				Input		the name or index of the form
//	checkBoxName		Input		the name of the check box
//	storeText			Input		the text that store ID serial
//
//  Return Value        :   None
//	Return Type         :   None
//--------------------------------------------------------------------------------
	var i;
	var len;
	var IDS;
	IDS="";
	
	len=document.forms[formKey].elements.length;
	
	for(i=0;i<len;i++){
		if(document.forms[formKey].elements[i].name==checkBoxName){			
			if(document.forms[formKey].elements[i].checked){				
				if(IDS==""){
					IDS=document.forms[formKey].elements[i].value;
				}else{
					IDS=IDS+","+document.forms[formKey].elements[i].value;
				}
			}
		}
	}
	
	//alert(IDS);	
	//将选择的文章的编号保存在Text(Hidden)中
	document.forms[formKey].elements[storeText].value=IDS;	
}

function selectAll(formKey,selectAll,checkBoxName){
//--------------------------------------------------------------------------------
//  Author 		:       Andy Wang
//  Version     Date			Description
//  1.0         07/14/2003   	选择“全选”则将Checkbox数组全选
//  Parameters		:
//  Name				Mode		Description
//	formKey				Input		the name or index of the form
//	selectAll			Input		the name of the "select all" check box
//	checkBoxName		Input		the name of the check box
//
//  Return Value        :   None
//	Return Type         :   None
//--------------------------------------------------------------------------------	
	var i;
	var len;
	var selectedStatus;

	len=document.forms[formKey].elements.length;	
	if(document.forms[formKey].elements[selectAll].checked){
		selectedStatus=true;
	}else{
		selectedStatus=false;
	}
	
	for(i=0;i<len-1;i++){
		if(document.forms[formKey].elements[i].name==checkBoxName){
			document.forms[formKey].elements[i].checked=selectedStatus ;
		}
	}		
}

function getFileName(fullPath){
//--------------------------------------------------------------------------------
//  Author 		:       Andy Wang
//  Version     Date			Description
//  1.0         07/14/2003   	Return file name according to the full path
//  Parameters		:
//  Name				Mode		Description
//	fullPath			Input		the full path of the file
//
//  Return Value        :   File name
//	Return Type         :   String
//--------------------------------------------------------------------------------	
	var index,len;
	var fileName;	
	len=fullPath.length;
	index=fullPath.lastIndexOf("\\");
	return(fullPath.substring(index+1,len).toLowerCase());
}

function getFilePostfix(fileName){
//--------------------------------------------------------------------------------
//  Author 		:       Andy Wang
//  Version     Date			Description
//  1.0         07/14/2003   	Return file postfix according to the file name
//  Parameters		:
//  Name				Mode		Description
//	fileName			Input		File name
//
//  Return Value        :   File postfix
//	Return Type         :   String
//--------------------------------------------------------------------------------	
	var index,len;	
	len=fileName.length;	
	index=fileName.lastIndexOf(".");	
	return(fileName.substring(index+1,len).toLowerCase());	
}



function StringReplace(theString,reStr,newStr){
//--------------------------------------------------------------------------------
//  Author 		:       Andy Wang
//  Version     Date			Description
//  1.0         05/21/2003   	Check the user name
//								UpperLetters,LowerLetters,number is default.
//  Parameters		:
//  Name				Mode		Description
//	theString			Input		The string to be replaced
//	reStr				Input		Replaced string
//	newStr				Input		New string
//
//  Return Value        :   The new string
//	Return Type         :   String
//--------------------------------------------------------------------------------	

	var i,len;
	var result=theString;
	
	len=theString.length;
	for(i=0;i<len;i++){
		result=result.replace(reStr,newStr);
	}	
	return result;
}


function CheckUserNameChar(formKey,elementName,SpecChar,minLen,maxLen,PromptString){
//--------------------------------------------------------------------------------
//  Author 		:       Andy Wang
//  Version     Date			Description
//  1.0         05/21/2003   	Check the user name
//								UpperLetters,LowerLetters,number is default.
//  Parameters		:
//  Name				Mode		Description
//	formKey				Input		the name or index of the form
//	elementName			Input		the name or element
//	minLen				Input		the min length
//	maxLen				Input		the max length, -1 : infinite
//	PromptString		Input		the prompted string
//
//  Return Value        :   true/false
//	Return Type         :   boolean
//--------------------------------------------------------------------------------	
	var UserNameChars;
	var UserName=document.forms[formKey].elements[elementName].value;
	var len;
	var beBlank=true;
	var beChartOK=true;
	
	//Get valid characters
	UserNameChars=Alphanumeric + SpecChar;
	
	len=UserName.length;
	
	//- must not be blank	
	for(i=0;i<len;i++){
		if(UserName.charAt(i)!=" "){
			beBlank=false;
			break;
		}
	}
	if(beBlank==true){
		alert(PromptString);
		setTextFocus(formKey,elementName);
		return false;
	}	
	
	//validate all characters
	for(i=0;i<len;i++){
		chr=UserName.charAt(i);
		if(UserNameChars.indexOf(chr)==-1){			
			beChartOK=false;
			break;
		}
	}
	if(!beChartOK){
		alert(PromptString);
		setTextFocus(formKey,elementName);
		return false;
	}	
	
	//- minimal length
	if(len<minLen){
		alert(PromptString);
		setTextFocus(formKey,elementName);
		return false;	
	}
	//- maximal length ,-1:infinite
	if(maxLen!=-1){
		if(len>maxLen){
			alert(PromptString);
			setTextFocus(formKey,elementName);
			return false;
		}
	}	
	
	return true;
}


function MustBeNumber(formKey,elementName,SpecChar,PromptString){
//--------------------------------------------------------------------------------
//  Author 		:       Andy Wang
//  Version     Date			Description
//  1.0         05/21/2003   	Check value of the text is number
//								
//  Parameters		:
//  Name				Mode		Description
//	formKey				Input		the name or index of the form
//	elementName			Input		the name or element
//	SpecChar			Input		included special characters
//	PromptString		Input		the prompted string
//
//  Return Value        :   true/false
//	Return Type         :   boolean
//--------------------------------------------------------------------------------	

	var i,len;
	var strValue=document.forms[formKey].elements[elementName].value;
	var beChartOK=true;
	var strNumbers=Numbers + " " + SpecChar;
	len=strValue.length;
	
	//Check each character
	for(i=0;i<len;i++){
		chr=strValue.charAt(i);
		if(strNumbers.indexOf(chr)==-1){			
			beChartOK=false;
		}
	}
	
	//Check whether is space string
	if(textIsBlank(formKey,elementName)) beChartOK=false;
	
	if(!beChartOK){
		alert(PromptString);
		setTextFocus(formKey,elementName);
		return false;
	}
	return true;	
}


function SelectControlArrayAll(formKey,elementName,allSelected){
//--------------------------------------------------------------------------------
//  Author 		:       Andy Wang
//  Version     Date			Description
//  1.0         05/21/2003   	Check value of the text is number
//								
//  Parameters		:
//  Name				Mode		Description
//	formKey				Input		the name or index of the form
//	elementName			Input		the name or element
//	allSelected			Input		all selected status
//
//  Return Value        :   none
//	Return Type         :   none
//--------------------------------------------------------------------------------
	var ele =document.forms[formKey].elements[elementName];
	var i,len;
	if(ele.length *	 0 !=0){		//Only one ,not control array
		ele.checked=allSelected;
	}else{
		len=ele.length ;
		for(i=0;i<len;i++){
			ele[i].checked=allSelected ;
		}	
	}
}

function CheckControlArray(formKey,elementName){
//--------------------------------------------------------------------------------
//  Author 		:       Andy Wang
//  Version     Date			Description
//  1.0         05/21/2003   	Check value of the text is number
//								
//  Parameters		:
//  Name				Mode		Description
//	formKey				Input		the name or index of the form
//	elementName			Input		the name or element
//
//  Return Value        :   none
//	Return Type         :   none
//--------------------------------------------------------------------------------
	var ele =document.forms[formKey].elements[elementName];
	var i,len;
	var blnOK=false;
	if(ele.length *	 0 !=0){		//Only one ,not control array
		blnOK=ele.checked;
	}else{
		len=ele.length ;
		for(i=0;i<len;i++){
			if(ele[i].checked){
				blnOK=true;
				break;
			}
		}	
	}
	return blnOK;
}


function SetControlArrayFocus(formKey,elementName){
//--------------------------------------------------------------------------------
//  Author 		:       Andy Wang
//  Version     Date			Description
//  1.0         05/21/2003   	Check value of the text is number
//								
//  Parameters		:
//  Name				Mode		Description
//	formKey				Input		the name or index of the form
//	elementName			Input		the name or element
//
//  Return Value        :   none
//	Return Type         :   none
//--------------------------------------------------------------------------------
	var ele =document.forms[formKey].elements[elementName];
	if(ele.length *	 0 !=0){		//Only one ,not control array
		ele.focus();
	}else{
		ele[0].focus();
	}
}
function SubmitAction(theform,action)
{

	if(!CheckControlArray(0,"chkID")){
		alert("对不起,请选择!");
		SetControlArrayFocus(0,"chkID");
		return false;
	}

//if(action=="DeleteSelected"){
if(action.indexOf("DeleteSelected")!=-1){
		if(!confirm("您确定要删除吗? ")) return false;	
	}
	
	theform.action="?action=" + action
	return true;
}
//选择图片
function SelectedImg(ImgPath,thisImg)
{
	//ImgPath = "../image/Club/Product/"
	ImgName =opener.document.all("ImgName");
	txtImgName = opener.document.all("txtImgName");
	if (ImgName != null)
	{
		ImgName.src = ImgPath + thisImg;
	}

	if (txtImgName != null)
	{
		txtImgName.value = thisImg
	}
	window.close();
}